Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
25.00% covered (danger)
25.00%
1 / 4
CRAP
60.00% covered (warning)
60.00%
9 / 15
Assets
0.00% covered (danger)
0.00%
0 / 1
25.00% covered (danger)
25.00%
1 / 4
12.10
60.00% covered (warning)
60.00%
9 / 15
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 make
0.00% covered (danger)
0.00%
0 / 1
4.12
50.00% covered (danger)
50.00%
3 / 6
 js
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 2
 css
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
<?php
namespace Eywa\File {
    use Eywa\Exception\Kedavra;
    use Eywa\Http\Request\Request;
    class Assets
    {
        /**
         *
         * The type filename
         *
         */
        private string $filename;
        /**
         *
         * The type of balise to create
         *
         */
        private string $type;
        /**
         *
         * Assets constructor.
         *
         * @param string $type
         * @param string $filename
         *
         * @throws Kedavra
         */
        public function __construct(string $type, string $filename)
        {
            not_in(['css','js'], $type, true, 'The type must be css or js');
            $this->filename = $filename;
            $this->type = $type;
        }
        /**
         *
         * Genrate the link
         *
         * @return string
         *
         * @throws Kedavra
         *
         */
        public function make(): string
        {
            switch ($this->type) {
                case 'css':
                    return sprintf('<link rel="stylesheet" href="%s">', $this->css());
                case 'js':
                    return sprintf('<script src="%s"></script>', $this->js());
                default:
                    return '';
            }
        }
        /**
         *
         *
         * Generate js url
         *
         * @return string
         *
         * @throws Kedavra
         *
         */
        private function js(): string
        {
            return https()  ? 'https://' . Request::make()->server()->get('SERVER_NAME') . '/js/' . $this->filename
                            : 'http://' . Request::make()->server()->get('SERVER_NAME') . '/js/' . $this->filename;
        }
        /**
         *
         * Generate css url
         *
         * @return string
         *
         * @throws Kedavra
         *
         */
        private function css(): string
        {
            return https()
                    ?
                        'https://' . Request::make()->server()->get('SERVER_NAME') . '/css/' . $this->filename
                    :   'http://' . Request::make()->server()->get('SERVER_NAME') . '/css/' . $this->filename;
        }
    }
}