ScopeRepository::create()   A
last analyzed

Complexity

Conditions 2
Paths 5

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 13
cts 13
cp 1
rs 9.584
c 0
b 0
f 0
cc 2
nc 5
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Passport\Repositories;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use DateTimeImmutable;
22
use Limoncello\Passport\Contracts\Entities\ScopeInterface;
23
use Limoncello\Passport\Contracts\Repositories\ScopeRepositoryInterface;
24
use Limoncello\Passport\Exceptions\RepositoryException;
25
26
/**
27
 * @package Limoncello\Passport
28
 */
29
abstract class ScopeRepository extends BaseRepository implements ScopeRepositoryInterface
30
{
31
    /**
32
     * @inheritdoc
33
     *
34
     * @throws RepositoryException
35
     */
36 2
    public function index(): array
37
    {
38
        try {
39 2
            return parent::indexResources();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (indexResources() instead of index()). Are you sure this is correct? If so, you might want to change this to $this->indexResources().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
40 1
        } catch (RepositoryException $exception) {
41 1
            $message = 'Reading scopes failed.';
42 1
            throw new RepositoryException($message, 0, $exception);
43
        }
44
    }
45
46
    /**
47
     * @inheritdoc
48
     *
49
     * @throws RepositoryException
50
     */
51 20 View Code Duplication
    public function create(ScopeInterface $scope): ScopeInterface
0 ignored issues
show
Duplication introduced by
This method 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...
52
    {
53
        try {
54
            $now    = $this->ignoreException(function (): DateTimeImmutable {
55 20
                return new DateTimeImmutable();
56 20
            });
57 20
            $schema = $this->getDatabaseSchema();
58 20
            $this->createResource([
0 ignored issues
show
Documentation introduced by
array($schema->getScopes...atedAtColumn() => $now) is of type array<string,*>, but the function expects a object<Limoncello\Passport\Repositories\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59 20
                $schema->getScopesIdentityColumn()    => $scope->getIdentifier(),
60 20
                $schema->getScopesDescriptionColumn() => $scope->getDescription(),
61 20
                $schema->getScopesCreatedAtColumn()   => $now,
62
            ]);
63
64 19
            $scope->setCreatedAt($now);
65
66 19
            return $scope;
67 1
        } catch (RepositoryException $exception) {
68 1
            $message = 'Scope creation failed.';
69 1
            throw new RepositoryException($message, 0, $exception);
70
        }
71
    }
72
73
    /**
74
     * @inheritdoc
75
     *
76
     * @throws RepositoryException
77
     */
78 2 View Code Duplication
    public function read(string $identifier): ScopeInterface
0 ignored issues
show
Duplication introduced by
This method 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...
79
    {
80
        try {
81 2
            return $this->readResource($identifier);
82 1
        } catch (RepositoryException $exception) {
83 1
            $message = 'Scope reading failed.';
84 1
            throw new RepositoryException($message, 0, $exception);
85
        }
86
    }
87
88
    /**
89
     * @inheritdoc
90
     *
91
     * @throws RepositoryException
92
     */
93 2 View Code Duplication
    public function update(ScopeInterface $scope): void
0 ignored issues
show
Duplication introduced by
This method 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...
94
    {
95
        try {
96
            $now    = $this->ignoreException(function (): DateTimeImmutable {
97 2
                return new DateTimeImmutable();
98 2
            });
99 2
            $schema = $this->getDatabaseSchema();
100 2
            $this->updateResource($scope->getIdentifier(), [
101 2
                $schema->getScopesDescriptionColumn() => $scope->getDescription(),
102 2
                $schema->getScopesUpdatedAtColumn()   => $now,
103
            ]);
104 1
            $scope->setUpdatedAt($now);
105 1
        } catch (RepositoryException $exception) {
106 1
            $message = 'Scope update failed.';
107 1
            throw new RepositoryException($message, 0, $exception);
108
        }
109
    }
110
111
    /**
112
     * @inheritdoc
113
     *
114
     * @throws RepositoryException
115
     */
116 2 View Code Duplication
    public function delete(string $identifier): void
0 ignored issues
show
Duplication introduced by
This method 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...
117
    {
118
        try {
119 2
            $this->deleteResource($identifier);
120 1
        } catch (RepositoryException $exception) {
121 1
            $message = 'Scope deletion failed.';
122 1
            throw new RepositoryException($message, 0, $exception);
123
        }
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129 26
    protected function getTableNameForWriting(): string
130
    {
131 26
        return $this->getDatabaseSchema()->getScopesTable();
132
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137 4
    protected function getPrimaryKeyName(): string
138
    {
139 4
        return $this->getDatabaseSchema()->getScopesIdentityColumn();
140
    }
141
}
142