Passed
Push — main ( 1d1c5b...83bc52 )
by Daniel
04:23
created

FavoriteManager::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 9.7666
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Favorite;
6
7
use Uxmp\Core\Orm\Model\UserInterface;
8
use Uxmp\Core\Orm\Repository\FavoriteRepositoryInterface;
9
10
final class FavoriteManager implements FavoriteManagerInterface
11
{
12 4
    public function __construct(
13
        private readonly FavoriteRepositoryInterface $favoriteRepository
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 13 at column 25
Loading history...
14
    ) {
15
    }
16
17 2
    public function add(
18
        FavoriteAbleInterface $obj,
19
        UserInterface $user,
20
    ): bool {
21 2
        $itemId = $obj->getId();
22 2
        $type = $obj->getType();
23
24 2
        $favorite = $this->favoriteRepository->findOneBy([
25
            'user' => $user,
26
            'item_id' => $itemId,
27
            'type' => $type,
28
        ]);
29
30 2
        if ($favorite === null) {
31 1
            $favorite = $this->favoriteRepository->prototype()
32 1
                ->setUser($user)
33 1
                ->setType($type)
34 1
                ->setItemId($itemId)
35 1
                ->setDate(new \DateTime());
36
37 1
            $this->favoriteRepository->save($favorite);
38
39 1
            return true;
40
        }
41
42 1
        return false;
43
    }
44
45 2
    public function remove(
46
        FavoriteAbleInterface $obj,
47
        UserInterface $user,
48
    ): bool {
49 2
        $favorite = $this->favoriteRepository->findOneBy([
50
            'user' => $user,
51 2
            'item_id' => $obj->getId(),
52 2
            'type' => $obj->getType(),
53
        ]);
54
55 2
        if ($favorite !== null) {
56 1
            $this->favoriteRepository->delete($favorite);
57
58 1
            return true;
59
        }
60
61 1
        return false;
62
    }
63
}
64