Completed
Push — master ( 78b3b4...1185ef )
by Fumio
02:21
created

Addon::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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 8
        } 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
     * set config value.
158
     *
159
     * @param string $key
160
     * @param mixed $value
161
     */
162
    public function setConfig($key, $value)
163
    {
164
        $this->config->set($key, $value);
165
    }
166
167
    /**
168
     * Get a lang resource name
169
     *
170
     * @param string $resource
171
     *
172
     * @return string
173
     */
174 1
    public function transName($resource)
175
    {
176 1
        return $this->name.'::'.$resource;
177
    }
178
179
    /**
180
     * Translate the given message.
181
     *
182
     * @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...
183
     * @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...
184
     * @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...
185
     * @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...
186
     * @return string
187
     */
188 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...
189
    {
190 1
        $args = func_get_args();
191 1
        $args[0] = $this->transName($args[0]);
192
193 1
        return call_user_func_array([$this->app['translator'], 'trans'], $args);
194
    }
195
196
    /**
197
     * Translates the given message based on a count.
198
     *
199
     * @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...
200
     * @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...
201
     * @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...
202
     * @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...
203
     * @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...
204
     * @return string
205
     */
206 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...
207
    {
208 1
         $args = func_get_args();
209 1
         $args[0] = $this->transName($args[0]);
210
211 1
         return call_user_func_array([$this->app['translator'], 'transChoice'], $args);
212
    }
213
214
    /**
215
     * Get a view resource name
216
     *
217
     * @param string $resource
218
     *
219
     * @return string
220
     */
221
    public function viewName($resource)
222
    {
223
        return $this->name.'::'.$resource;
224
    }
225
226
    /**
227
     * @param string $view
228
     * @param array $data
229
     * @param array $mergeData
230
     *
231
     * @return \Illuminate\View\View
232
     */
233
    public function view($view, $data = [], $mergeData = [])
234
    {
235
        return $this->app['view']->make($this->viewname($view), $data, $mergeData);
236
    }
237
238
    /**
239
     * Get a spec resource name
240
     *
241
     * @param string $resource
242
     *
243
     * @return string
244
     */
245
    public function specName($resource)
246
    {
247
        return $this->name.'::'.$resource;
248
    }
249
250
    /**
251
     * Get spec.
252
     *
253
     * @param string $path
254
     *
255
     * @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec
256
     */
257
    public function spec($path)
258
    {
259
        return $this->app[SpecFactory::class]->make($this->specName($path));
260
    }
261
}
262