RepositoryManager::getRules()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Konsulting\Laravel\RuleRepository;
4
5
use Illuminate\Support\Str;
6
use Konsulting\Laravel\RuleRepository\Contracts\RuleRepository;
7
use Konsulting\Laravel\RuleRepository\Exceptions\NonExistentStateException;
8
9
class RepositoryManager
10
{
11
    /**
12
     * The repository instance.
13
     *
14
     * @var RuleRepository
15
     */
16
    protected $repository;
17
18
    /**
19
     * Set the repository instance.
20
     *
21
     * @param RuleRepository $repository
22
     */
23 12
    public function __construct(RuleRepository $repository)
24
    {
25 12
        $this->repository = $repository;
26 12
    }
27
28
    /**
29
     * Return the rules for the given state. Get the default state if not specified.
30
     *
31
     * @param string $state
32
     * @return array
33
     * @throws NonExistentStateException
34
     */
35 15
    public function getRules($state = null)
36
    {
37 15
        $method = Str::camel($state ?: 'default');
38
39 15
        if ( ! method_exists($this->repository, $method)) {
40 1
            throw new NonExistentStateException($method);
41
        }
42
43 14
        return array_merge(
44 14
            $this->repository->default(),
45 14
            is_array($this->repository->$method()) ? $this->repository->$method() : []
46
        );
47
    }
48
}
49