| Total Complexity | 4 |
| Total Lines | 29 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 10 | final readonly class CatalogAdder implements CatalogAdderInterface |
||
|
|
|||
| 11 | { |
||
| 12 | 3 | public function __construct( |
|
| 13 | private CatalogRepositoryInterface $catalogRepository |
||
| 14 | ) { |
||
| 15 | 3 | } |
|
| 16 | |||
| 17 | 3 | public function add(Interactor $io, string $path): void |
|
| 18 | { |
||
| 19 | 3 | $realPath = realpath($path); |
|
| 20 | |||
| 21 | 3 | if ($realPath === false) { |
|
| 22 | 1 | $io->error(sprintf('`%s` is not a valid path', $path), true); |
|
| 23 | 1 | return; |
|
| 24 | } |
||
| 25 | |||
| 26 | 2 | $catalog = $this->catalogRepository->findOneBy(['path' => $realPath]); |
|
| 27 | |||
| 28 | 2 | if ($catalog !== null) { |
|
| 29 | 1 | $io->error(sprintf('`%s` has already been added as a catalog', $realPath), true); |
|
| 30 | 1 | return; |
|
| 31 | } |
||
| 32 | |||
| 33 | 1 | $catalog = $this->catalogRepository->prototype(); |
|
| 34 | 1 | $catalog->setPath($realPath); |
|
| 35 | |||
| 36 | 1 | $this->catalogRepository->save($catalog); |
|
| 37 | |||
| 38 | 1 | $io->ok(sprintf('Catalog has been added with id `%d`', $catalog->getId()), true); |
|
| 39 | } |
||
| 41 |