FavoriteManager   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 43
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setFavorite() 0 8 2
A getFavorite() 0 7 2
A __construct() 0 3 1
1
<?php
2
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace DembeloMain\Model;
21
22
use DembeloMain\Document\Textnode;
23
use DembeloMain\Document\Topic;
24
use DembeloMain\Document\User;
25
use Symfony\Component\HttpFoundation\Session\Session;
26
27
/**
28
 * Class FavoriteManager
29
 */
30
class FavoriteManager
31
{
32
    /* @var Session */
33
    private $session;
34
35
    /**
36
     * FavoriteManager constructor.
37
     * @param Session $session
38
     */
39 5
    public function __construct(Session $session)
40
    {
41 5
        $this->session = $session;
42 5
    }
43
44
    /**
45
     * sets a favorite textnode for a topic
46
     * @param Textnode  $textnode
47
     * @param User|null $user
48
     */
49 4
    public function setFavorite(Textnode $textnode, User $user = null): void
50
    {
51 4
        if (null === $user) {
52 2
            $this->session->set('favorite_'.$textnode->getTopicId(), $textnode->getArbitraryId());
53
54 2
            return;
55
        }
56 2
        $user->setFavorite($textnode->getTopicId(), $textnode->getArbitraryId());
57 2
    }
58
59
    /**
60
     * gets a favorite textnode for a topic
61
     * @param Topic     $topic
62
     * @param User|null $user
63
     *
64
     * @return string|null
65
     */
66 5
    public function getFavorite(Topic $topic, User $user = null): ?string
67
    {
68 5
        if (null === $user) {
69 3
            return $this->session->get('favorite_'.$topic->getId());
70
        }
71
72 2
        return $user->getFavorite($topic->getId());
73
    }
74
}
75