Issues (6)

SlimX/Models/Tables/AbstractTable.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace SlimX\Models\Tables;
4
5
use Psr\Log\LoggerInterface;
0 ignored issues
show
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use RedBeanPHP\OODBBean;
7
use RedBeanPHP\R;
8
9
/**
10
 * Table abstraction
11
 */
12
abstract class AbstractTable
13
{
14
    protected $tableName;
15
    protected $logger;
16
    protected $cache;
17
18
    /**
19
     * Default controller.
20
     *
21
     * @param LoggerInterface $logger Logger to be used by the class.
22
     */
23
    public function __construct(LoggerInterface $logger)
24
    {
25
        $this->logger = $logger;
26
        $this->cache = [];
27
    }
28
29
    /**
30
     * Find one row, by ID.
31
     *
32
     * @param int $id Row ID.
33
     *
34
     * @return ?OODBBean Row, if found, NULL otherwise.
35
     */
36
    public function findOneById(int $id): ?OODBBean
37
    {
38
        if (!isset($this->cache[$id])) {
39
            $this->logger->debug(
40
                get_class($this) . '::findOneById cache miss ' . $id
41
            );
42
            $bean = R::findOne($this->tableName, 'id = ? AND active = ?', [$id, 1]);
43
            if (count($this->cache) < 1024) {
44
                $this->cache[$id] = $bean;
45
            }
46
        } else {
47
            $this->logger->debug(
48
                get_class($this) . '::findOneById cache hit ' . $id
49
            );
50
            $bean = $this->cache[$id];
51
        }
52
53
        return $bean;
54
    }
55
56
    /**
57
     * Store (insert or update) and load bean.
58
     *
59
     * @param OODBBean $bean Bean to be stored.
60
     *
61
     * @return OODBBean Bean freshly loaded from database.
62
     */
63
    public function storeAndLoad(OODBBean $bean): OODBBean
64
    {
65
        return R::load($this->tableName, (int) R::store($bean));
66
    }
67
}
68