Completed
Pull Request — master (#14)
by Yann
01:59
created

DeleteArchivist::archive()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 23
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
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
 * @author Yann Eugoné <[email protected]>
10
 */
11
class DeleteArchivist implements ArchivistInterface
12
{
13
    /**
14
     * @var EntityRepository
15
     */
16
    private $tokenRepository;
17
18
    /**
19
     * @param EntityRepository $tokenRepository
20
     */
21
    public function __construct(EntityRepository $tokenRepository)
22
    {
23
        $this->tokenRepository = $tokenRepository;
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function archive($purpose = null, DateTime $before = null)
30
    {
31
        if (null !== $before) {
32
            @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...
33
                'The "before" argument of the "'.__METHOD__
34
                .'" method is deprecated since version 2.2 and will be removed in 3.0.',
35
                E_USER_DEPRECATED
36
            );
37
        }
38
39
        $builder = $this->tokenRepository->createQueryBuilder('token')
40
            ->delete($this->tokenRepository->getClassName(), 'token');
41
42
        $builder
43
            ->where($builder->expr()->lt('token.keepUntil', ':now'))
44
            ->setParameter('now', new DateTime())
45
        ;
46
47
        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...
48
            $builder
49
                ->andWhere($builder->expr()->eq('token.purpose', ':purpose'))
50
                ->setParameter('purpose', $purpose)
51
            ;
52
        }
53
54
        return intval($builder->getQuery()->execute());
55
    }
56
}
57