Passed
Pull Request — stable (#145)
by Nuno
02:33
created

Container::abort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelZero\Framework;
4
5
use RuntimeException;
6
use Illuminate\Container\Container as BaseContainer;
7
use LaravelZero\Framework\Exceptions\ConsoleException;
8
use LaravelZero\Framework\Exceptions\NotImplementedException;
9
use Symfony\Component\Console\Exception\CommandNotFoundException;
10
use Illuminate\Contracts\Foundation\Application as LaravelApplication;
11
12
/**
13
 * This is the Laravel Zero Framework container class.
14
 *
15
 * @author Nuno Maduro <[email protected]>
16
 */
17
class Container extends BaseContainer implements LaravelApplication
18
{
19
    /**
20
     * A custom callback used to configure Monolog.
21
     *
22
     * @var callable|null
23
     */
24
    protected $monologConfigurator;
25
26
    /**
27
     * The application namespace.
28
     *
29
     * @var string
30
     */
31
    protected $namespace;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    public function version()
37
    {
38 2
        return config('app.version');
39
    }
40
41
    /**
42
     * Get the base path of the Laravel installation.
43
     *
44
     * @param  string $path
45
     *
46
     * @return string
47
     */
48 51
    public function basePath($path = '')
49
    {
50 51
        return BASE_PATH.($path ? DIRECTORY_SEPARATOR.$path : $path);
51
    }
52
53
    /**
54
     * Get the path to the application configuration files.
55
     *
56
     * @param  string $path
57
     *
58
     * @return string
59
     */
60 51
    public function configPath($path = '')
61
    {
62 51
        return BASE_PATH.DIRECTORY_SEPARATOR.'config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
63
    }
64
65
    /**
66
     * Get the path to the database directory.
67
     *
68
     * @param  string $path
69
     *
70
     * @return string
71
     */
72 51
    public function databasePath($path = '')
73
    {
74 51
        return (BASE_PATH.DIRECTORY_SEPARATOR.'database').($path ? DIRECTORY_SEPARATOR.$path : $path);
75
    }
76
77
    /**
78
     * Get the path to the language files.
79
     *
80
     * @return string
81
     */
82 1
    public function langPath()
83
    {
84 1
        return $this->resourcePath('lang');
85
    }
86
87
    /**
88
     * Get the path to the resources directory.
89
     *
90
     * @param  string $path
91
     *
92
     * @return string
93
     */
94 3
    public function resourcePath($path = '')
95
    {
96 3
        return BASE_PATH.DIRECTORY_SEPARATOR.'resources'.($path ? DIRECTORY_SEPARATOR.$path : $path);
97
    }
98
99
    /**
100
     * Get the path to the storage directory.
101
     *
102
     * @return string
103
     */
104 51
    public function storagePath()
105
    {
106 51
        return BASE_PATH.DIRECTORY_SEPARATOR.'storage';
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 51
    public function environment()
113
    {
114 51
        return config('app.production') ? 'production' : 'development';
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 1
    public function runningInConsole()
121
    {
122 1
        return true;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 51
    public function getNamespace()
129
    {
130 51
        if (! is_null($this->namespace)) {
131 2
            return $this->namespace;
132
        }
133 51
        $composer = json_decode(file_get_contents(base_path('composer.json')), true);
134 51
        foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) {
135 51
            foreach ((array) $path as $pathChoice) {
136 51
                if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) {
137 51
                    return $this->namespace = $namespace;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespace can also be of type integer. However, the property $namespace is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
138
                }
139
            }
140
        }
141
        throw new RuntimeException('Unable to detect application namespace.');
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 1
    public function isDownForMaintenance()
148
    {
149 1
        return false;
150
    }
151
152
    /**
153
     * Throw an Console Exception with the given data unless the given condition is true.
154
     *
155
     * @param  int $code
156
     * @param  string $message
157
     * @param  array $headers
158
     * @return void
159
     *
160
     * @throws \Symfony\Component\Console\Exception\CommandNotFoundException
161
     * @throws \LaravelZero\Framework\Contracts\Exceptions\ConsoleException
162
     */
163 1
    public function abort($code, $message = '', array $headers = [])
164
    {
165 1
        if ($code == 404) {
166 1
            throw new CommandNotFoundException($message);
167
        }
168
169 1
        throw new ConsoleException($code, $message, $headers);
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 1
    public function registerConfiguredProviders()
176
    {
177 1
        throw new NotImplementedException;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 1
    public function register($provider, $options = [], $force = false)
184
    {
185 1
        throw new NotImplementedException;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 1
    public function registerDeferredProvider($provider, $service = null)
192
    {
193 1
        throw new NotImplementedException;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 1
    public function boot()
200
    {
201 1
        throw new NotImplementedException;
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 1
    public function booting($callback)
208
    {
209 1
        throw new NotImplementedException;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215 1
    public function booted($callback)
216
    {
217 1
        throw new NotImplementedException;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 1
    public function getCachedServicesPath()
224
    {
225 1
        throw new NotImplementedException;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function getCachedPackagesPath()
232
    {
233
        throw new NotImplementedException;
234
    }
235
236
    /**
237
     * Define a callback to be used to configure Monolog.
238
     *
239
     * @param  callable $callback
240
     *
241
     * @return $this
242
     */
243 1
    public function configureMonologUsing(callable $callback)
244
    {
245 1
        $this->monologConfigurator = $callback;
246
247 1
        return $this;
248
    }
249
250
    /**
251
     * Determine if the application has a custom Monolog configurator.
252
     *
253
     * @return bool
254
     */
255 1
    public function hasMonologConfigurator()
256
    {
257 1
        return ! is_null($this->monologConfigurator);
258
    }
259
260
    /**
261
     * Get the custom Monolog configurator for the application.
262
     *
263
     * @return callable
264
     */
265 1
    public function getMonologConfigurator()
266
    {
267 1
        return $this->monologConfigurator;
268
    }
269
}
270