DatabaseRulesTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 44
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 6 2
A existAll() 0 6 2
A unique() 0 6 2
1
<?php declare (strict_types = 1);
2
3
namespace Limoncello\Flute\Validation\Rules;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Flute\Validation\JsonApi\Rules\ExistInDbTableMultipleWithDoctrineRule;
22
use Limoncello\Flute\Validation\JsonApi\Rules\ExistInDbTableSingleWithDoctrineRule;
23
use Limoncello\Flute\Validation\JsonApi\Rules\UniqueInDbTableSingleWithDoctrineRule;
24
use Limoncello\Validation\Contracts\Rules\RuleInterface;
25
use Limoncello\Validation\Rules\Generic\AndOperator;
26
27
/**
28
 * @package Limoncello\Flute
29
 */
30
trait DatabaseRulesTrait
31
{
32
    /**
33
     * @param string             $tableName
34
     * @param string             $primaryName
35
     * @param RuleInterface|null $next
36
     *
37
     * @return RuleInterface
38
     */
39 32
    public static function exists(string $tableName, string $primaryName, RuleInterface $next = null): RuleInterface
40
    {
41 32
        $primary = new ExistInDbTableSingleWithDoctrineRule($tableName, $primaryName);
42
43 32
        return $next === null ? $primary : new AndOperator($primary, $next);
44
    }
45
46
    /**
47
     * @param string             $tableName
48
     * @param string             $primaryName
49
     * @param RuleInterface|null $next
50
     *
51
     * @return RuleInterface
52
     */
53 32
    public static function existAll(string $tableName, string $primaryName, RuleInterface $next = null): RuleInterface
54
    {
55 32
        $primary = new ExistInDbTableMultipleWithDoctrineRule($tableName, $primaryName);
56
57 32
        return $next === null ? $primary : new AndOperator($primary, $next);
58
    }
59
60
    /**
61
     * @param string             $tableName
62
     * @param string             $primaryName
63
     * @param RuleInterface|null $next
64
     *
65
     * @return RuleInterface
66
     */
67 2
    public static function unique(string $tableName, string $primaryName, RuleInterface $next = null): RuleInterface
68
    {
69 2
        $primary = new UniqueInDbTableSingleWithDoctrineRule($tableName, $primaryName);
70
71 2
        return $next === null ? $primary : new AndOperator($primary, $next);
72
    }
73
}
74