Passed
Push — master ( 472b4c...8e27d6 )
by Gabriel
04:44
created

HasPathsTrait::initPaths()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\Migrations\Config\Traits;
4
5
use ByTIC\Migrations\Utility\Helper;
6
7
/**
8
 * Trait HasPathsTrait
9
 * @package ByTIC\Migrations\Config\Traits
10
 */
11
trait HasPathsTrait
12
{
13
    /**
14
     * @param $path
15
     */
16 1
    public function addBasePath($path)
17
    {
18 1
        $this->setPath($path . DIRECTORY_SEPARATOR . 'migrations', 'migrations');
19 1
        $this->setPath($path . DIRECTORY_SEPARATOR . 'seeds', 'seeds');
20 1
    }
21
22
    /**
23
     * @param string $type
24
     */
25 2
    public function initPaths($type = 'migrations')
26
    {
27 2
        $paths = [];
28 2
        if (in_array($type, ['migrations', 'seeds'])) {
29 2
            $test = Helper::normalizePath(Helper::getBasePath(), 'database', $type);
30 2
            if (is_dir($test)) {
31 2
                $paths[] = $test;
32
            }
33
        }
34 2
        $this->setPath($paths, $type);
35 2
    }
36
37
    /**
38
     * @param $type
39
     * @param $path
40
     */
41 3
    public function setPath($path, $type = 'migrations')
42
    {
43 3
        $path = is_array($path) ? $path : [$path];
44 3
        $this->params['paths'][$type] = $path;
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45 3
    }
46
47
    /**
48
     * Register a custom migration path.
49
     *
50
     * @param string $path
51
     * @param string $type
52
     * @return void
53
     */
54 1
    public function path($path, $type = 'migrations')
55
    {
56 1
        $existing = $this->params['paths'][$type];
57 1
        $existing = is_array($existing) ? $existing : [$existing];
58 1
        $this->params['paths'][$type] = array_unique(array_merge($existing, [$path]));
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59 1
    }
60
61
    /**
62
     * @param string $type
63
     * @return mixed
64
     */
65 2
    public function paths($type = 'migrations')
66
    {
67 2
        return $this->params['paths'][$type];
68
    }
69
}