Completed
Pull Request — master (#117)
by Kristof
04:39
created

Security   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 2
dl 67
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A allowsUpdateWithCdbXml() 4 4 1
A allowsUpdates() 4 4 1
B currentUiTIDUserCanEditPlace() 26 26 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\Place;
7
8
use CultuurNet\SymfonySecurityOAuthUitid\User;
9
use CultuurNet\UDB3\Event\SecurityInterface;
10
use CultuurNet\UDB3\Place\ReadModel\Permission\PermissionQueryInterface;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
12
use ValueObjects\String\String;
13
14 View Code Duplication
class Security implements SecurityInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var PermissionQueryInterface
18
     */
19
    private $permissionRepository;
20
21
    /**
22
     * @var TokenStorageInterface
23
     */
24
    private $tokenStorage;
25
26
    public function __construct(
27
        TokenStorageInterface $tokenStorage,
28
        PermissionQueryInterface $permissionRepository
29
    ) {
30
        $this->tokenStorage = $tokenStorage;
31
        $this->permissionRepository = $permissionRepository;
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function allowsUpdateWithCdbXml(String $eventId)
38
    {
39
        return $this->currentUiTIDUserCanEditPlace($eventId);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function allowsUpdates(String $eventId)
46
    {
47
        return $this->currentUiTIDUserCanEditPlace($eventId);
48
    }
49
50
    /**
51
     * @param String $placeId
52
     * @return bool
53
     */
54
    private function currentUiTIDUserCanEditPlace(String $placeId)
55
    {
56
        $token = $this->tokenStorage->getToken();
57
58
        if (!$token) {
59
            return false;
60
        }
61
62
        $user = $token->getUser();
63
64
        if ($user instanceof User) {
0 ignored issues
show
Bug introduced by
The class CultuurNet\SymfonySecurityOAuthUitid\User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
65
            $userId = new String($user->getUid());
66
        } else if ($user instanceof \CultuurNet\UiTIDProvider\User\User) {
0 ignored issues
show
Bug introduced by
The class CultuurNet\UiTIDProvider\User\User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
            $userId = new String($user->id);
68
        }
69
70
        if (!isset($userId)) {
71
            return false;
72
        }
73
74
        $editableEvents = $this->permissionRepository->getEditablePlaces(
75
            $userId
76
        );
77
78
        return in_array($placeId, $editableEvents);
79
    }
80
}
81