Completed
Pull Request — master (#91)
by Jeroen
01:33
created

ControllerNotFoundSolutionProvider::canSolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Throwable;
6
use Illuminate\Support\Str;
7
use Facade\IgnitionContracts\HasSolutionsForThrowable;
8
use Facade\Ignition\Solutions\CreateControllerSolution;
9
use Illuminate\Contracts\Container\BindingResolutionException;
10
11
class ControllerNotFoundSolutionProvider implements HasSolutionsForThrowable
12
{
13
    public function canSolve(Throwable $throwable): bool
14
    {
15
        if (! $throwable instanceof BindingResolutionException) {
16
            return false;
17
        }
18
19
        return  $this->isMissingControllerError($throwable->getMessage());
20
    }
21
22
    protected function isMissingControllerError(string $message): bool
23
    {
24
        return Str::startsWith($message, 'Target class [App\\Http\\Controllers\\');
25
    }
26
27
    public function getSolutions(Throwable $throwable): array
28
    {
29
        $controller = $this->getController($throwable->getMessage());
30
31
        return [
32
            new CreateControllerSolution($controller),
33
        ];
34
    }
35
36
    private function getController(string $message): string
37
    {
38
        return preg_replace('/^.*\[(.*?)\].*$/', '$1', $message);
39
    }
40
}
41