ControlsRivetsTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 11
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 63
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFileAttributes() 0 6 1
A getConfig() 0 4 1
A getFileProcessor() 0 4 1
A getStorage() 0 4 1
A getFilePath() 0 4 1
A createRivet() 0 15 3
A updateRivet() 0 17 3
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