Completed
Pull Request — master (#32)
by Yann
03:30
created

Archive/DeleteArchivist.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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(string $purpose = null): int
32
    {
33
        $builder = $this->tokenRepository->createQueryBuilder('token')
34
            ->delete($this->tokenRepository->getClassName(), 'token');
35
36
        $builder
37
            ->where($builder->expr()->lt('token.keepUntil', ':now'))
38
            ->setParameter('now', new DateTime())
39
        ;
40
41
        if ($purpose) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $purpose of type null|string 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...
42
            $builder
43
                ->andWhere($builder->expr()->eq('token.purpose', ':purpose'))
44
                ->setParameter('purpose', $purpose)
45
            ;
46
        }
47
48
        return intval($builder->getQuery()->execute());
49
    }
50
}
51