Issues (3)

src/Identifier/Resolver/OrmResolver.php (1 issue)

1
<?php
2
declare(strict_types=1);
3
namespace Phauthentic\Authentication\Identifier\Resolver;
4
5
use ArrayAccess;
6
use Phauthentic\Authentication\Identifier\Resolver\ResolverInterface;
7
use Cake\Core\InstanceConfigTrait;
8
use Cake\ORM\Locator\LocatorAwareTrait;
9
10
class OrmResolver implements ResolverInterface
11
{
12
13
    use InstanceConfigTrait;
14
    use LocatorAwareTrait;
15
16
    /**
17
     * Default configuration.
18
     * - `userModel` The alias for users table, defaults to Users.
19
     * - `finder` The finder method to use to fetch user record. Defaults to 'all'.
20
     *   You can set finder name as string or an array where key is finder name and value
21
     *   is an array passed to `Table::find()` options.
22
     *   E.g. ['finderName' => ['some_finder_option' => 'some_value']]
23
     *
24
     * @var array
25
     */
26
    protected $_defaultConfig = [
27
        'userModel' => 'Users',
28
        'finder' => 'all',
29
    ];
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param array $config Config array.
35
     */
36 25
    public function __construct(array $config = [])
37
    {
38 25
        $this->setConfig($config);
39 25
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44 9
    public function find(array $conditions): ?ArrayAccess
45
    {
46 9
        $table = $this->getTableLocator()->get($this->getConfig('userModel'));
47
48 9
        $query = $table->query();
49 9
        $finders = (array)$this->getConfig('finder');
50 9
        foreach ($finders as $finder => $options) {
51 9
            if (is_string($options)) {
52 9
                $query->find($options);
53
            } else {
54 9
                $query->find($finder, $options);
55
            }
56
        }
57
58 9
        $where = [];
59 9
        foreach ($conditions as $field => $value) {
60 9
            $field = $table->aliasField($field);
61 9
            if (is_array($value)) {
62 1
                $field = $field . ' IN';
63
            }
64 9
            $where[$field] = $value;
65
        }
66
67 9
        return $query->where($where)->first();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->where($where)->first() could return the type array which is incompatible with the type-hinted return null|ArrayAccess. Consider adding an additional type-check to rule them out.
Loading history...
68
    }
69
}
70