Completed
Push — master ( 0563c6...cb42e4 )
by Yann
02:20
created

DeleteArchivist::archive()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 21
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
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
     * @var EntityRepository
20
     */
21
    private $tokenUsageRepository;
22
23
    /**
24
     * @param EntityRepository $tokenRepository
25
     * @param EntityRepository $tokenUsageRepository
26
     */
27
    public function __construct(EntityRepository $tokenRepository, EntityRepository $tokenUsageRepository)
28
    {
29
        $this->tokenRepository = $tokenRepository;
30
        $this->tokenUsageRepository = $tokenUsageRepository;
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public function archive($purpose = null, DateTime $before = null)
37
    {
38
        $builder = $this->tokenRepository->createQueryBuilder('token')
39
            ->delete($this->tokenRepository->getClassName(), 'token');
40
41
        $subBuilder = $this->tokenUsageRepository->createQueryBuilder('token_usage')
42
            ->select('token_usage.id');
43
44
        $builder->where($builder->expr()->in('token.id', $subBuilder->getDQL()));
45
46
        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...
47
            $builder
48
                ->andWhere($builder->expr()->eq('token.purpose', ':purpose'))
49
                ->setParameter('purpose', $purpose)
50
            ;
51
        }
52
53
        if ($before) {
54
            $builder
55
                ->andWhere($builder->expr()->lt('token.createdAt', ':before'))
56
                ->setParameter('before', $before)
57
            ;
58
        }
59
60
        return intval($builder->getQuery()->execute());
61
    }
62
}
63