1 | <?php |
||
18 | final class ChangeCodingStandardEventSubscriber implements EventSubscriberInterface |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @var Configuration |
||
23 | */ |
||
24 | private $configuration; |
||
25 | |||
26 | /** |
||
27 | * @var CodeStyleInterface |
||
28 | */ |
||
29 | private $codeStyle; |
||
30 | |||
31 | |||
32 | 6 | public function __construct(Configuration $configuration, CodeStyleInterface $codeStyle) |
|
33 | { |
||
34 | 6 | $this->codeStyle = $codeStyle; |
|
35 | 6 | $this->configuration = $configuration; |
|
36 | 6 | } |
|
37 | |||
38 | |||
39 | /** |
||
40 | * @return array |
||
41 | */ |
||
42 | 2 | public static function getSubscribedEvents() |
|
43 | { |
||
44 | 2 | return [ConsoleEvents::TERMINATE => 'applyCodingStyle']; |
|
45 | } |
||
46 | |||
47 | |||
48 | 6 | public function applyCodingStyle(ConsoleTerminateEvent $event) |
|
49 | { |
||
50 | 6 | $command = $event->getCommand(); |
|
51 | 6 | if ( ! $this->isAllowedCommand($command->getName())) { |
|
52 | 4 | return; |
|
53 | } |
||
54 | |||
55 | 2 | $filename = $this->getCurrentMigrationFileName(); |
|
56 | 2 | if (file_exists($filename)) { |
|
57 | 2 | $this->codeStyle->applyForFile($filename); |
|
58 | } |
||
59 | 2 | } |
|
60 | |||
61 | |||
62 | /** |
||
63 | * @param string $name |
||
64 | * @return bool |
||
65 | */ |
||
66 | 6 | private function isAllowedCommand($name) |
|
70 | |||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | 2 | private function getCurrentMigrationFileName() |
|
76 | { |
||
77 | 2 | $version = $this->getCurrentVersionName(); |
|
78 | |||
79 | 2 | $i = 0; |
|
80 | 2 | while ( ! file_exists($this->getMigrationFileByVersion($version)) && $i <= 10) { |
|
81 | $version--; |
||
82 | $i++; |
||
83 | } |
||
84 | |||
85 | 2 | $path = $this->getMigrationFileByVersion($version); |
|
86 | 2 | return $path; |
|
87 | } |
||
88 | |||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | 2 | private function getCurrentVersionName() |
|
97 | |||
98 | |||
99 | /** |
||
100 | * @param string $version |
||
101 | * @return string |
||
102 | */ |
||
103 | 2 | private function getMigrationFileByVersion($version) |
|
108 | |||
109 | } |
||
110 |