Completed
Push — master ( 1ca3ad...78b3b4 )
by Fumio
02:09
created

Addon::loadConfigurationFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Config\Repository;
7
use RuntimeException;
8
9
class Addon
10
{
11
    /**
12
     * @param \Illuminate\Contracts\Foundation\Application  $app
13
     * @param string $path
14
     *
15
     * @return static
16
     */
17 8
    public static function create($app, $path)
18
    {
19 8
        $pathComponents = explode('/', $path);
20
21 8
        $name = $pathComponents[count($pathComponents) - 1];
22
23 8
        $config = static::loadAddonConfig($path, $name);
24
25 8
        return new static($app, $name, $path, $config);
26
    }
27
28
    /**
29
     * @param string $path
30
     * @param string $name
31
     *
32
     * @return array
33
     */
34 8
    protected static function loadAddonConfig($path, $name)
35
    {
36 8
        if (file_exists($path.'/addon.php')) {
37 8
            $config = require $path.'/addon.php';
38
        } else {
39
            throw new RuntimeException("No such config file for addon '$name', need 'addon.php'.");
40
        }
41
42 8
        $version = array_get($config, 'version', 5);
43 8
        if ($version != 5) {
44
            throw new RuntimeException($version.': Illigal addon version.');
45
        }
46
47 8
        return $config;
48
    }
49
50
    /**
51
     * @var \Illuminate\Contracts\Foundation\Application
52
     */
53
    protected $app;
54
55
    /**
56
     * @var string
57
     */
58
    protected $name;
59
60
    /**
61
     * @var string
62
     */
63
    protected $path;
64
65
    /**
66
     * @var \Illuminate\Contracts\Config\Repository
67
     */
68
    protected $config;
69
70
    /**
71
     * @param \Illuminate\Contracts\Foundation\Application  $app
72
     * @param string  $name
73
     * @param string  $path
74
     * @param array   $config
75
     */
76 16
    public function __construct($app, $name, $path, array $config)
77
    {
78 16
        $this->app = $app;
79 16
        $this->name = $name;
80 16
        $this->path = $path;
81 16
        $this->config = new Repository();
82 16
        $this->config->set('addon', $config);
83 16
    }
84
85
    /**
86
     * get name.
87
     *
88
     * @return string
89
     */
90 11
    public function name()
91
    {
92 11
        return $this->name;
93
    }
94
95
    /**
96
     * get fullpath.
97
     *
98
     * @param string $path
99
     *
100
     * @return string
101
     */
102 7
    public function path($path = null)
103
    {
104 7
        if (func_num_args() == 0) {
105 3
            return $this->path;
106
        } else {
107 6
            return $this->path.'/'.$path;
108
        }
109
    }
110
111
    /**
112
     * get relative path.
113
     *
114
     * @param \Illuminate\Contracts\Foundation\Application $app
115
     *
116
     * @return string
117
     */
118 8
    public function relativePath(Application $app)
119
    {
120 8
        return substr($this->path, strlen($app->basePath()) + 1);
121
    }
122
123
    /**
124
     * get version.
125
     *
126
     * @return int
127
     */
128 2
    public function version()
129
    {
130 2
        return $this->config('addon.version', 5);
131
    }
132
133
    /**
134
     * get PHP namespace.
135
     *
136
     * @return string
137
     */
138 8
    public function phpNamespace()
139
    {
140 8
        return trim($this->config('addon.namespace', ''), '\\');
141
    }
142
143
    /**
144
     * get config value.
145
     *
146
     * @param string $key
147
     * @param mixed $default
148
     *
149
     * @return mixed
150
     */
151 11
    public function config($key, $default = null)
152
    {
153 11
        return $this->config->get($key, $default);
154
    }
155
156
    /**
157
     * Get a lang resource name
158
     *
159
     * @param string $resource
160
     *
161
     * @return string
162
     */
163 1
    public function transName($resource)
164
    {
165 1
        return $this->name.'::'.$resource;
166
    }
167
168
    /**
169
     * Translate the given message.
170
     *
171
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
172
     * @param array $parameters
0 ignored issues
show
Bug introduced by
There is no parameter named $parameters. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
173
     * @param string $domain
0 ignored issues
show
Bug introduced by
There is no parameter named $domain. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
174
     * @param string $locale
0 ignored issues
show
Bug introduced by
There is no parameter named $locale. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
175
     * @return string
176
     */
177 1 View Code Duplication
    public function trans()
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...
178
    {
179 1
        $args = func_get_args();
180 1
        $args[0] = $this->transName($args[0]);
181
182 1
        return call_user_func_array([$this->app['translator'], 'trans'], $args);
183
    }
184
185
    /**
186
     * Translates the given message based on a count.
187
     *
188
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
189
     * @param int $number
0 ignored issues
show
Bug introduced by
There is no parameter named $number. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
190
     * @param array $parameters
0 ignored issues
show
Bug introduced by
There is no parameter named $parameters. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
191
     * @param string $domain
0 ignored issues
show
Bug introduced by
There is no parameter named $domain. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
192
     * @param string $locale
0 ignored issues
show
Bug introduced by
There is no parameter named $locale. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
193
     * @return string
194
     */
195 1 View Code Duplication
    public function transChoice()
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...
196
    {
197 1
         $args = func_get_args();
198 1
         $args[0] = $this->transName($args[0]);
199
200 1
         return call_user_func_array([$this->app['translator'], 'transChoice'], $args);
201
    }
202
203
    /**
204
     * Get a view resource name
205
     *
206
     * @param string $resource
207
     *
208
     * @return string
209
     */
210
    public function viewName($resource)
211
    {
212
        return $this->name.'::'.$resource;
213
    }
214
215
    /**
216
     * @param string $view
217
     * @param array $data
218
     * @param array $mergeData
219
     *
220
     * @return \Illuminate\View\View
221
     */
222
    public function view($view, $data = [], $mergeData = [])
223
    {
224
        return $this->app['view']->make($this->viewname($view), $data, $mergeData);
225
    }
226
227
    /**
228
     * Get a spec resource name
229
     *
230
     * @param string $resource
231
     *
232
     * @return string
233
     */
234
    public function specName($resource)
235
    {
236
        return $this->name.'::'.$resource;
237
    }
238
239
    /**
240
     * Get spec.
241
     *
242
     * @param string $path
243
     *
244
     * @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec
245
     */
246
    public function spec($path)
247
    {
248
        return $this->app[SpecFactory::class]->make($this->specName($path));
249
    }
250
}
251