| Total Complexity | 9 |
| Total Lines | 56 |
| Duplicated Lines | 0 % |
| Coverage | 92% |
| Changes | 0 | ||
| 1 | <?php |
||
| 22 | class MigrationDirectoryHelper |
||
| 23 | { |
||
| 24 | /** @var Configuration */ |
||
| 25 | private $configuration; |
||
| 26 | |||
| 27 | 14 | public function __construct(Configuration $configuration) |
|
| 28 | { |
||
| 29 | 14 | $this->configuration = $configuration; |
|
| 30 | 14 | } |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @throws DirectoryDoesNotExist |
||
| 34 | */ |
||
| 35 | 13 | public function getMigrationDirectory() : string |
|
| 36 | { |
||
| 37 | 13 | $dir = $this->configuration->getMigrationsDirectory(); |
|
| 38 | 13 | $dir = $dir ?? getcwd(); |
|
| 39 | |||
| 40 | 13 | assert($dir !== false, 'Unable to determine current working directory.'); |
|
| 41 | |||
| 42 | 13 | $dir = rtrim($dir, '/'); |
|
| 43 | |||
| 44 | 13 | if (! file_exists($dir)) { |
|
| 45 | 1 | throw DirectoryDoesNotExist::new($dir); |
|
| 46 | } |
||
| 47 | |||
| 48 | 12 | if ($this->configuration->areMigrationsOrganizedByYear()) { |
|
| 49 | 3 | $dir .= $this->appendDir(date('Y')); |
|
| 50 | } |
||
| 51 | |||
| 52 | 12 | if ($this->configuration->areMigrationsOrganizedByYearAndMonth()) { |
|
| 53 | 2 | $dir .= $this->appendDir(date('m')); |
|
| 54 | } |
||
| 55 | |||
| 56 | 12 | $this->createDirIfNotExists($dir); |
|
| 57 | |||
| 58 | 12 | return $dir; |
|
| 59 | } |
||
| 60 | |||
| 61 | 3 | private function appendDir(string $dir) : string |
|
| 64 | } |
||
| 65 | |||
| 66 | 12 | private function createDirIfNotExists(string $dir) : void |
|
| 67 | { |
||
| 68 | 12 | if (file_exists($dir)) { |
|
| 69 | 9 | return; |
|
| 70 | } |
||
| 71 | |||
| 72 | 3 | mkdir($dir, 0755, true); |
|
| 73 | 3 | } |
|
| 74 | |||
| 75 | public function getName() : string |
||
| 78 | } |
||
| 79 | } |
||
| 80 |