Passed
Push — main ( 02442c...4ee80d )
by Dimitri
13:27
created

Unique::check()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.9
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Validation\Rules;
13
14
use BlitzPHP\Contracts\Database\ConnectionInterface;
15
use BlitzPHP\Models\BaseEntity;
16
use BlitzPHP\Wolke\Model;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Wolke\Model 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...
17
use Dimtrovich\Validation\Rules\AbstractRule;
18
19
class Unique extends AbstractRule
20
{
21
    protected $message        = ':attribute :value has been used';
22
    protected $fillableParams = ['table', 'column', 'ignore'];
23
24
	/**
25
     * The name of the ID column.
26
     */
27
    protected string $idColumn = 'id';
28
29
	protected string $deletedAtColumn = '';
30
31
32
    public function __construct(protected ConnectionInterface $db)
33
    {
34
    }
35
36
	/**
37
     * Ignore the given ID during the unique check.
38
     */
39
    public function ignore(mixed $id, ?string $idColumn = null): self
40
    {
41
		if (class_exists(Model::class) && $id instanceof BaseEntity) {
42
            return $this->ignoreEntity($id, $idColumn);
43
        }
44
		
45
        $this->params['ignore'] = $id;
46
        $this->idColumn         = $idColumn ?? 'id';
47
		
48
49
        return $this;
50
    }
51
52
	/**
53
     * Ignore the given model during the unique check.
54
     */
55
    public function ignoreEntity(BaseEntity $entity, ?string $idColumn = null): self
56
    {
57
		$this->idColumn         = $idColumn ?? $entity->getKeyName();
58
        $this->params['ignore'] = $entity->{$this->idColumn};
59
60
        return $this;
61
    }
62
63
	/**
64
     * Ignore soft deleted models during the unique check.
65
     */
66
    public function withoutTrashed(string $deletedAtColumn = 'deleted_at'): self
67
    {
68
		$this->deletedAtColumn = $deletedAtColumn;
69
70
        return $this;
71
    }
72
73
    public function check($value): bool
74
    {
75
        $this->requireParameters(['table']);
76
77
        $table  = $this->parameter('table');
78
        $ignore = $this->parameter('ignore');
79
        $column = $this->parameter('column');
80
        $column = $column ?: $this->getAttribute()->getKey();
81
82
		$builder = $this->db->table($table)->where($column, $value);
83
84
		if ($ignore) {
85
			$builder->where($this->idColumn . ' !=', $ignore);
86
		}
87
		if ('' !== $this->deletedAtColumn) {
88
			$builder->where($this->deletedAtColumn . ' IS NULL');
89
		}
90
91
        return $builder->count() === 0;
92
    }
93
}
94