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

isMissingControllerReflectionError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
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 ReflectionException;
7
use Illuminate\Support\Str;
8
use Facade\IgnitionContracts\HasSolutionsForThrowable;
9
use Facade\Ignition\Solutions\CreateControllerSolution;
10
use Illuminate\Contracts\Container\BindingResolutionException;
11
12
class ControllerNotFoundSolutionProvider implements HasSolutionsForThrowable
13
{
14
    public function canSolve(Throwable $throwable): bool
15
    {
16
        if ($throwable instanceof BindingResolutionException) {
17
            return $this->isMissingControllerBindingError($throwable->getMessage());
18
        }
19
20
        if ($throwable instanceof ReflectionException) {
21
            return $this->isMissingControllerReflectionError($throwable->getMessage());
22
        }
23
24
        return false;
25
    }
26
27
    protected function isMissingControllerBindingError(string $message): bool
28
    {
29
        return Str::startsWith($message, 'Target class [App\\Http\\Controllers\\');
30
    }
31
32
    protected function isMissingControllerReflectionError(string $message): bool
33
    {
34
        return Str::startsWith($message, 'Class App\Http\Controllers\\') && Str::endsWith($message, 'Controller does not exist');
35
    }
36
37
    public function getSolutions(Throwable $throwable): array
38
    {
39
        $controller = $this->getController($throwable);
40
41
        return [
42
            new CreateControllerSolution($controller),
43
        ];
44
    }
45
46
    private function getController(Throwable $throwable): string
47
    {
48
        // Class App\Http\Controllers\SomeController does not exist
49
        if ($throwable instanceof ReflectionException) {
50
            return preg_replace('/^.*(App\\\\[^\s]+).*$/', '$1', $throwable->getMessage());
51
        }
52
53
        return preg_replace('/^.*\[(.*?)\].*$/', '$1', $throwable->getMessage());
54
    }
55
}
56