CdnHelper::getConfigurations()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Vinelab\Cdn;
4
5
use Illuminate\Config\Repository;
6
use Vinelab\Cdn\Contracts\CdnHelperInterface;
7
use Vinelab\Cdn\Exceptions\MissingConfigurationException;
8
use Vinelab\Cdn\Exceptions\MissingConfigurationFileException;
9
10
/**
11
 * Class CdnHelper
12
 * Helper class containing shared functions.
13
 *
14
 * @category General Helper
15
 *
16
 * @author  Mahmoud Zalt <[email protected]>
17
 */
18
class CdnHelper implements CdnHelperInterface
19
{
20
    /**
21
     * An object of the 'Repository' class that allows reading the laravel config files.
22
     *
23
     * @var \Illuminate\Config\Repository
24
     */
25
    protected $configurations;
26
27
    /**
28
     * @param \Illuminate\Config\Repository $configurations
29
     */
30
    public function __construct(Repository $configurations)
31
    {
32
        $this->configurations = $configurations;
33
    }
34
35
    /**
36
     * Check if the config file exist and return it or
37
     * throw an exception.
38
     *
39
     * @return array
40
     *
41
     * @throws Exceptions\MissingConfigurationFileException
42
     */
43
    public function getConfigurations()
44
    {
45
        $configurations = $this->configurations->get('cdn');
46
47
        if (!$configurations) {
48
            throw new MissingConfigurationFileException("CDN 'config file' (cdn.php) not found");
49
        }
50
51
        return $configurations;
52
    }
53
54
    /**
55
     * Checks for any required configuration is missed.
56
     *
57
     * @param $configuration
58
     * @param $required
59
     *
60
     * @throws \Vinelab\Cdn\Exceptions\MissingConfigurationException
61
     */
62 View Code Duplication
    public function validate($configuration, $required)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        // search for any null or empty field to throw an exception
65
        $missing = '';
66
        foreach ($configuration as $key => $value) {
67
            if (in_array($key, $required) &&
68
                (empty($value) || $value == null || $value == '')
69
            ) {
70
                $missing .= ' '.$key;
71
            }
72
        }
73
74
        if ($missing) {
75
            throw new MissingConfigurationException('Missed Configuration:'.$missing);
76
        }
77
    }
78
79
    /**
80
     * Take url as string and return it parsed object.
81
     *
82
     * @param $url
83
     *
84
     * @return mixed
85
     */
86
    public function parseUrl($url)
87
    {
88
        return parse_url($url);
89
    }
90
91
    /**
92
     * check if a string starts with a string.
93
     *
94
     * @param $with
95
     * @param $str
96
     *
97
     * @return bool
98
     */
99
    public function startsWith($with, $str)
100
    {
101
        return (substr($str, 0, strlen($with)) === $with);
102
    }
103
104
    /**
105
     * remove any extra slashes '/' from the path.
106
     *
107
     * @param $path
108
     *
109
     * @return string
110
     */
111
    public function cleanPath($path)
112
    {
113
        return rtrim(ltrim($path, '/'), '/');
114
    }
115
}
116