CdnHelper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 16.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 3
dl 16
loc 98
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfigurations() 0 10 2
B validate() 16 16 7
A parseUrl() 0 4 1
A startsWith() 0 4 1
A cleanPath() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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