Completed
Pull Request — master (#33)
by
unknown
09:54
created

getCurrentVersionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Zenify
7
 * Copyright (c) 2014 Tomas Votruba (http://tomasvotruba.cz)
8
 */
9
10
namespace Zenify\DoctrineMigrations\EventSubscriber;
11
12
use Doctrine\DBAL\Migrations\Configuration\Configuration;
13
use Doctrine\DBAL\Migrations\Tools\Console\Helper\MigrationDirectoryHelper;
14
use Symfony\Component\Console\ConsoleEvents;
15
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Zenify\DoctrineMigrations\Contract\CodeStyle\CodeStyleInterface;
18
19
20
final class ChangeCodingStandardEventSubscriber implements EventSubscriberInterface
21
{
22
23
	/**
24
	 * @var Configuration
25
	 */
26
	private $configuration;
27
28
	/**
29
	 * @var CodeStyleInterface
30
	 */
31
	private $codeStyle;
32
33
34 6
	public function __construct(Configuration $configuration, CodeStyleInterface $codeStyle)
35
	{
36 6
		$this->codeStyle = $codeStyle;
37 6
		$this->configuration = $configuration;
38 6
	}
39
40
41 2
	public static function getSubscribedEvents() : array
42
	{
43 2
		return [ConsoleEvents::TERMINATE => 'applyCodingStyle'];
44
	}
45
46
47 6
	public function applyCodingStyle(ConsoleTerminateEvent $event)
48
	{
49 6
		$command = $event->getCommand();
50 6
		if ( ! $this->isAllowedCommand($command->getName())) {
51 4
			return;
52
		}
53
54 2
		$filename = $this->getCurrentMigrationFileName();
55 2
		if (file_exists($filename)) {
56 2
			$this->codeStyle->applyForFile($filename);
57
		}
58 2
	}
59
60
61 6
	private function isAllowedCommand(string $name) : bool
62
	{
63 6
		return in_array($name, ['migrations:generate', 'migrations:diff']);
64
	}
65
66
67 2
	private function getCurrentMigrationFileName() : string
68
	{
69 2
		$version = $this->getCurrentVersionName();
70
71 2
		$i = 0;
72 2
		while ( ! file_exists($this->getMigrationFileByVersion((string) $version)) && $i <= 10) {
73
			$version--;
74
			$i++;
75
		}
76
77 2
		$path = $this->getMigrationFileByVersion((string) $version);
78 2
		return $path;
79
	}
80
81
82 2
	private function getCurrentVersionName() : string
83
	{
84 2
		return date('YmdHis');
85
	}
86
87
88 2
	private function getMigrationFileByVersion(string $version) : string
89
	{
90 2
		$migrationDirectoryHelper = new MigrationDirectoryHelper($this->configuration);
91 2
		return $migrationDirectoryHelper->getMigrationDirectory() . '/Version' . $version . '.php';
92
	}
93
94
}
95