Passed
Push — main ( 7dcca1...e70bce )
by Daniel
04:22
created

CatalogAdder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 29
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 0
wmc 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Catalog\Manage;
6
7
use Ahc\Cli\IO\Interactor;
8
use Uxmp\Core\Orm\Repository\CatalogRepositoryInterface;
9
10
final readonly class CatalogAdder implements CatalogAdderInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 10 at column 6
Loading history...
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
    }
40
}
41