DeleteArchivist::archive()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
ccs 0
cts 23
cp 0
rs 9.488
cc 3
nc 4
nop 2
crap 12
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Archive;
4
5
use DateTime;
6
use Doctrine\ORM\EntityRepository;
7
8
/**
9
 * This archivist is removing all outdated tokens based on the `keepUntil` property.
10
 *
11
 * @author Yann Eugoné <[email protected]>
12
 */
13
class DeleteArchivist implements ArchivistInterface
14
{
15
    /**
16
     * @var EntityRepository
17
     */
18
    private $tokenRepository;
19
20
    /**
21
     * @param EntityRepository $tokenRepository The token entity repository
22
     */
23
    public function __construct(EntityRepository $tokenRepository)
24
    {
25
        $this->tokenRepository = $tokenRepository;
26
    }
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function archive($purpose = null, DateTime $before = null)
32
    {
33
        if (null !== $before) {
34
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
35
                'The "before" argument of the "'.__METHOD__
36
                .'" method is deprecated since version 2.2 and will be removed in 3.0.',
37
                E_USER_DEPRECATED
38
            );
39
        }
40
41
        $builder = $this->tokenRepository->createQueryBuilder('token')
42
            ->delete($this->tokenRepository->getClassName(), 'token');
43
44
        $builder
45
            ->where($builder->expr()->lt('token.keepUntil', ':now'))
46
            ->setParameter('now', new DateTime())
47
        ;
48
49
        if ($purpose) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $purpose of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
50
            $builder
51
                ->andWhere($builder->expr()->eq('token.purpose', ':purpose'))
52
                ->setParameter('purpose', $purpose)
53
            ;
54
        }
55
56
        return intval($builder->getQuery()->execute());
57
    }
58
}
59