ControlsRivetsTrait::getConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Luminark\Rivet\Traits;
4
5
use Luminark\Rivet\Models\Rivet;
6
use Luminark\Rivet\Facades\FileProcessor;
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Support\Facades\Storage;
9
10
trait ControlsRivetsTrait
11
{
12
    protected function createRivet($class, $data)
13
    {
14
        $fileAttributes = $this->getFileAttributes($class);
15
        $rivet = new $class(array_except($data, $fileAttributes));
16
        $fileProcessor = $this->getFileProcessor();
17
        foreach ($fileAttributes as $attribute) {
18
            if (array_has($data, $attribute)) {
19
                $rivet->$attribute = $fileProcessor->processFile($rivet, $data[$attribute]);
20
            }
21
        }
22
23
        $rivet->save();
24
25
        return $rivet;
26
    }
27
28
    protected function updateRivet(Rivet $rivet, $data)
29
    {
30
        $fileAttributes = $this->getFileAttributes(get_class($rivet));
31
        $fileProcessor = $this->getFileProcessor();
32
        $oldFiles = [];
33
        foreach ($fileAttributes as $attribute) {
34
            if (array_has($data, $attribute)) {
35
                $oldFiles[] = $this->getFilePath($rivet, $attribute);
36
                $rivet->$attribute = $fileProcessor->processFile($rivet, $data[$attribute]);
37
            }
38
        }
39
        $rivet->fill(array_except($data, $fileAttributes));
40
41
        $rivet->save();
42
43
        $this->getStorage()->delete($oldFiles);
44
    }
45
46
    protected function getFileAttributes($class)
47
    {
48
        return $this->getConfig()->get(
49
            'luminark.rivet.' . $class . '.file_attributes', []
50
        );
51
    }
52
53
    protected function getConfig()
54
    {
55
        return Config::getFacadeRoot();
56
    }
57
58
    protected function getFileProcessor()
59
    {
60
        return FileProcessor::getFacadeRoot();
61
    }
62
63
    protected function getStorage()
64
    {
65
        return Storage::getFacadeRoot();
66
    }
67
68
    protected function getFilePath(Rivet $rivet, $attribute)
69
    {
70
        return object_get($rivet->$attribute, 'path');
71
    }
72
}
73