Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.33% covered (danger)
8.33%
1 / 12
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ServiceTrait
8.33% covered (danger)
8.33%
1 / 12
33.33% covered (danger)
33.33%
1 / 3
71.39
0.00% covered (danger)
0.00%
0 / 1
 getService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isInUserServices
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3/**
4 * This file is part of the MADIS - RGPD Management application.
5 *
6 * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace App\Application\Traits\Model;
25
26use App\Domain\User\Model\Service;
27use App\Domain\User\Model\User;
28
29trait ServiceTrait
30{
31    /**
32     * @var Service|null
33     */
34    private $service;
35
36    public function getService(): ?Service
37    {
38        return $this->service;
39    }
40
41    public function setService(?Service $service = null): void
42    {
43        $this->service = $service;
44    }
45
46    public function isInUserServices(User $user): bool
47    {
48        if (false == $user->getCollectivity()->getIsServicesEnabled()) {
49            return true;
50        }
51        if (!$this->getService()) {
52            return true;
53        }
54        if (0 === $user->getServices()->count()) {
55            return true;
56        }
57        foreach ($user->getServices() as $service) {
58            if ($this->getService() && $service->getId() == $this->getService()->getId()) {
59                return true;
60            }
61        }
62
63        return false;
64    }
65}