Completed
Push — develop ( af0cfc...a6c09c )
by Abdelrahman
02:05
created

EloquentPresenceVerifier::model()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Verifiers;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Validation\DatabasePresenceVerifier;
9
use Illuminate\Database\ConnectionResolverInterface;
10
11
class EloquentPresenceVerifier extends DatabasePresenceVerifier
12
{
13
    /**
14
     * The eloquent model instance.
15
     *
16
     * @var \Illuminate\Database\Eloquent\Model
17
     */
18
    protected $model;
19
20
    /**
21
     * Create a new eloquent presence verifier instance.
22
     *
23
     * @param \Illuminate\Database\ConnectionResolverInterface $db
24
     * @param \Illuminate\Database\Eloquent\Model              $model
25
     *
26
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
27
     */
28
    public function __construct(ConnectionResolverInterface $db, Model $model)
29
    {
30
        parent::__construct($db);
31
32
        $this->model = $model;
33
    }
34
35
    /**
36
     * Count the number of objects in a collection having the given value.
37
     *
38
     * @param  string      $collection
39
     * @param  string      $column
40
     * @param  string      $value
41
     * @param  int|null    $excludeId
42
     * @param  string|null $idColumn
43
     * @param  array       $extra
44
     *
45
     * @return int
46
     */
47
    public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = [])
48
    {
49
        $query = $this->model($collection)->where($column, '=', $value);
50
51
        if (! is_null($excludeId) && $excludeId !== 'NULL') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $excludeId (integer) and 'NULL' (string) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
52
            $query->where($idColumn ?: 'id', '<>', $excludeId);
53
        }
54
55
        return $this->addConditions($query, $extra)->count();
56
    }
57
58
    /**
59
     * Count the number of objects in a collection with the given values.
60
     *
61
     * @param  string $collection
62
     * @param  string $column
63
     * @param  array  $values
64
     * @param  array  $extra
65
     *
66
     * @return int
67
     */
68
    public function getMultiCount($collection, $column, array $values, array $extra = [])
69
    {
70
        $query = $this->model($collection)->whereIn($column, $values);
71
72
        return $this->addConditions($query, $extra)->count();
73
    }
74
75
    /**
76
     * @param $collection
77
     *
78
     * @return Model
79
     */
80
    private function model($collection)
81
    {
82
        return $this->model->setConnection($this->connection)->setTable($collection)->useWritePdo();
83
    }
84
}
85