Passed
Push — master ( 4a146d...3e1455 )
by Peter
02:17
created

PageCategoryCache   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 43
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A hasAnyChangedSince() 0 21 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Databases\Queries;
6
7
use Opulence\Databases\ConnectionPools\ConnectionPool;
8
use Opulence\QueryBuilders\Conditions\ConditionFactory;
9
use Opulence\QueryBuilders\MySql\QueryBuilder;
10
11
class PageCategoryCache
12
{
13
    /** @var ConnectionPool */
14
    protected $connectionPool;
15
16
    /**
17
     * PageCategoryCache constructor.
18
     *
19
     * @param ConnectionPool $connectionPool
20
     */
21
    public function __construct(ConnectionPool $connectionPool)
22
    {
23
        $this->connectionPool = $connectionPool;
24
    }
25
26
    /**
27
     * @param string[] $identifiers
28
     * @param string   $cacheTime
29
     *
30
     * @return bool
31
     * @throws \Opulence\QueryBuilders\InvalidQueryException
32
     */
33
    public function hasAnyChangedSince(array $identifiers, string $cacheTime): bool
34
    {
35
        $conditions = new ConditionFactory();
36
        $query      = (new QueryBuilder())
37
            ->select('COUNT(*) AS count')
38
            ->from('pages')
39
            ->leftJoin('page_categories', 'page_categories', 'page_categories.id = pages.category_id')
40
            ->where('pages.deleted = 0')
41
            ->andWhere($conditions->in('page_categories.identifier', $identifiers))
42
            ->andWhere('pages.updated_at > ?')
43
            ->addUnnamedPlaceholderValue($cacheTime, \PDO::PARAM_STR)
44
        ;
45
46
        $connection = $this->connectionPool->getReadConnection();
47
        $statement  = $connection->prepare($query->getSql());
48
        $statement->bindValues($query->getParameters());
49
        if (!$statement->execute()) {
50
            return true;
51
        }
52
53
        return $statement->fetchColumn() > 0;
54
    }
55
}
56