Seeder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 28%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 0
dl 0
loc 95
ccs 7
cts 25
cp 0.28
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSeedFile() 0 15 3
A getCached() 0 18 3
A getOverwrittenFilePath() 0 4 1
A getSavedFile() 0 9 1
1
<?php namespace VojtaSvoboda\Reservations\Updates\Classes;
2
3
use Exception;
4
use Seeder as BaseSeeder;
5
use System\Models\File;
6
7
class Seeder extends BaseSeeder
8
{
9
    /** @var string $seedFileName */
10
    protected $seedFileName;
11
12
    /** @var $cache Cache for all objects */
13
    private $cache;
14
15
    /**
16
     * Return dummy seed or resources seed.
17
     *
18
     * @param $defaultPath
19
     *
20
     * @return string
21
     *
22
     * @throws Exception
23
     */
24 27
    protected function getSeedFile($defaultPath)
25
    {
26
        // if dummy file is overwritten
27 27
        $overwritterPath = $this->getOverwrittenFilePath($defaultPath);
28 27
        if (file_exists($overwritterPath)) {
29
            return $overwritterPath;
30
        }
31
32
        // otherwise return dummy data
33 27
        if (file_exists($defaultPath)) {
34 27
            return $defaultPath;
35
        }
36
37
        throw new Exception('Seed file ' . $this->seedFileName . ' not found!');
38
    }
39
40
    /**
41
     * Get item from cache or from $object reference.
42
     *
43
     * @param string $object
44
     * @param string $key
45
     * @param string $value
46
     * @param bool $createIfNotExists
47
     *
48
     * @return mixed
49
     */
50
    protected function getCached($object, $key, $value, $createIfNotExists = false)
51
    {
52
        $namespace = get_class($object);
53
        $cache = $this->cache;
54
        if (isset($cache[$namespace][$key][$value])) {
55
            return $cache[$namespace][$key][$value];
56
        }
57
58
        $item = $object::where($key, $value)->first();
59
        $this->cache[$namespace][$key][$value] = $item;
60
61
        // if not exists, create
62
        if ($createIfNotExists) {
63
            throw new \BadMethodCallException('createIfNotExists parameter is not implemented');
64
        }
65
66
        return $item;
67
    }
68
69
    /**
70
     * Get folder, where seed can be overwritten.
71
     *
72
     * - default folder e.g. /plugins/vojtasvoboda/reservations/updates/sources/products.yaml
73
     * - overwritten e.g.  /resources/vojtasvoboda/reservations/updates/sources/products.yaml
74
     *
75
     * @param string $defaultPath Default folder path.
76
     *
77
     * @return string
78
     */
79 27
    private function getOverwrittenFilePath($defaultPath)
80
    {
81 27
        return str_replace('plugins', 'resources', $defaultPath);
82
    }
83
84
    /**
85
     * Create file from path, save it and return File object
86
     *
87
     * @param string $path File path.
88
     * @param bool $public Should be file public or not?
89
     *
90
     * @return File
91
     */
92
    protected function getSavedFile($path, $public = true)
93
    {
94
        $file = new File();
95
        $file->is_public = $public;
96
        $file->fromFile($path);
97
        $file->save();
98
99
        return $file;
100
    }
101
}
102