Passed
Push — main ( e933de...5ef56a )
by Daniel
03:35
created

CatalogAdder::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 3
rs 9.8666
c 1
b 0
f 0
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 class CatalogAdder implements CatalogAdderInterface
11
{
12 3
    public function __construct(
13
        private readonly 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