Completed
Pull Request — master (#114)
by Bart
10:00
created

Volumes   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 67
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecords() 0 4 1
D import() 0 35 9
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use \Craft;
6
use craft\base\VolumeInterface;
7
use craft\volumes\Local;
8
9
/**
10
 * Schematic Asset Sources Service.
11
 *
12
 * Sync Craft Setups.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2018, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @see      http://www.nerds.company
19
 */
20
class Volumes extends Base
21
{
22
    //==============================================================================================================
23
    //================================================  EXPORT  ====================================================
24
    //==============================================================================================================
25
26
    /**
27
     * Get all asset transforms
28
     *
29
     * @return VolumeInterface[]
30
     */
31
    protected function getRecords()
32
    {
33
        return Craft::$app->volumes->getAllVolumes();
34
    }
35
36
    //==============================================================================================================
37
    //================================================  IMPORT  ====================================================
38
    //==============================================================================================================
39
40
41
    /**
42
     * Import asset volumes.
43
     *
44
     * @TODO Export volume class
45
     *
46
     * @param array $volumeDefinitions
47
     * @param bool  $force
48
     *
49
     * @return Result
50
     */
51
    public function import(array $volumeDefinitions, $force = false)
52
    {
53
        $recordsByHandle = [];
54
        foreach ($this->getRecords() as $record) {
55
            $recordsByHandle[$record->handle] = $record;
0 ignored issues
show
Bug introduced by
Accessing handle on the interface craft\base\VolumeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
56
        }
57
58
        foreach ($volumeDefinitions as $handle => $definition) {
59
            $record = new Local();
60
            if (array_key_exists($handle, $recordsByHandle)) {
61
                $record = $recordsByHandle[$handle];
62
            }
63
            $record->setAttributes($definition);
64
            if (Craft::$app->volumes->saveVolume($record)) {
65
                Craft::info('Imported volume '.$handle, 'schematic');
66
            } else {
67
                Craft::warning('Error importing volume '.$handle, 'schematic');
68
                foreach ($record->getErrors() as $errors) {
69
                    foreach ($errors as $error) {
70
                        var_dump($error);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($error); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
71
                        Craft::error($error, 'schematic');
72
                    }
73
                }
74
            }
75
            unset($recordsByHandle[$handle]);
76
        }
77
78
        if ($force) {
79
            // Delete volumes not in definitions
80
            foreach ($recordsByHandle as $handle => $record) {
81
                Craft::info('Deleting volume '.$handle, 'schematic');
82
                Craft::$app->volumes->deleteVolume($record);
83
            }
84
        }
85
    }
86
}
87