Completed
Pull Request — master (#42)
by James
01:15
created

UpdateViewNameSolution   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 96
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSolutionTitle() 0 4 1
A getDocumentationLinks() 0 4 1
A getSolutionActionDescription() 0 4 1
A getRunButtonText() 0 4 1
A getSolutionDescription() 0 4 1
A getRunParameters() 0 8 1
A isRunnable() 0 4 1
A run() 0 7 2
B updateViewName() 0 36 8
1
<?php
2
3
namespace Facade\Ignition\Solutions;
4
5
use Facade\IgnitionContracts\RunnableSolution;
6
7
class UpdateViewNameSolution implements RunnableSolution
8
{
9
    private $missingView;
10
    private $suggestedView;
11
12
    public function __construct($missingView = null, $suggestedView = null, $controllerPath = null)
13
    {
14
        $this->missingView = $missingView;
15
        $this->suggestedView = $suggestedView;
16
        $this->controllerPath = $controllerPath;
17
    }
18
19
    public function getSolutionTitle(): string
20
    {
21
        return $this->missingView.' was not found.';
22
    }
23
24
    public function getDocumentationLinks(): array
25
    {
26
        return [];
27
    }
28
29
    public function getSolutionActionDescription(): string
30
    {
31
        return 'Did you mean `'.$this->suggestedView.'`?';
32
    }
33
34
    public function getRunButtonText(): string
35
    {
36
        return 'Update view name';
37
    }
38
39
    public function getSolutionDescription(): string
40
    {
41
        return '';
42
    }
43
44
    public function getRunParameters(): array
45
    {
46
        return [
47
            'missingView' => $this->missingView,
48
            'suggestedView' => $this->suggestedView,
49
            'controllerPath' => $this->controllerPath,
50
        ];
51
    }
52
53
    public function isRunnable()
54
    {
55
        return $this->updateViewName($this->getRunParameters()) !== false;
56
    }
57
58
    public function run(array $parameters = [])
59
    {
60
        $output = $this->updateViewName($parameters);
61
        if ($output !== false) {
62
            file_put_contents(app_path().$parameters['controllerPath'], $output);
63
        }
64
    }
65
66
    public function updateViewName(array $parameters = [])
67
    {
68
        if (strpos($parameters['controllerPath'], 'ignition/tests/Solutions') !== false) {
69
            $file = $parameters['controllerPath'];
70
        } else {
71
            $file = app_path().$parameters['controllerPath'];
72
        }
73
        if (! is_file($file)) {
74
            return false;
75
        }
76
        $contents = file_get_contents($file);
77
        $tokens = token_get_all($contents);
78
        $expectedTokens = collect($tokens)->map(function ($token) use ($parameters) {
79
            if ($token[0] === T_CONSTANT_ENCAPSED_STRING && (
80
                $token[1] == "'".$parameters['missingView']."'" ||
81
                $token[1] == '"'.$parameters['missingView'].'"'
82
            )) {
83
                $token[1] = "'".$parameters['suggestedView']."'";
84
            }
85
86
            return $token;
87
        })->toArray();
88
89
        $newContents = collect($expectedTokens)->map(function ($token) {
90
            return is_array($token) ? $token[1] : $token;
91
        })->implode('');
92
93
        $newTokens = token_get_all($newContents);
94
95
        // If we're generating a file that doesn't mean the same thing then don't allow
96
        if ($expectedTokens !== $newTokens) {
97
            return false;
98
        }
99
100
        return $newContents;
101
    }
102
}
103