Completed
Push — master ( 8523c1...304fd8 )
by Bart
07:18
created

AssetsField::populate()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 12

Duplication

Lines 10
Ratio 50 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 10
loc 20
ccs 0
cts 15
cp 0
rs 8.8571
cc 5
eloc 12
nc 9
nop 4
crap 30
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Models;
4
5
use Craft\Craft;
6
use Craft\FieldModel;
7
use Craft\FieldGroupModel;
8
9
/**
10
 * Schematic Assets Field Model.
11
 *
12
 * A schematic field model for mapping asset data
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @link      http://www.nerds.company
19
 */
20
class AssetsField extends Field
21
{
22
    /**
23
     * @param FieldModel $field
24
     * @param $includeContext
25
     *
26
     * @return array
27
     */
28
    public function getDefinition(FieldModel $field, $includeContext)
29
    {
30
        $definition = parent::getDefinition($field, $includeContext);
31
        $settings = $definition['settings'];
32
33 View Code Duplication
        if (array_key_exists('defaultUploadLocationSource', $settings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
            $defaultUploadLocationSourceId = $settings['defaultUploadLocationSource'];
35
            $defaultUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeById($defaultUploadLocationSourceId);
36
            $settings['defaultUploadLocationSource'] = $defaultUploadLocationSource ? $defaultUploadLocationSource->handle : '';
37
        }
38
39 View Code Duplication
        if (array_key_exists('singleUploadLocationSource', $settings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
            $singleUploadLocationSourceId = $settings['singleUploadLocationSource'];
41
            $singleUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeById($singleUploadLocationSourceId);
42
            $settings['singleUploadLocationSource'] = $singleUploadLocationSource ? $singleUploadLocationSource->handle : '';
43
        }
44
45
        $definition['settings'] = $settings;
46
47
        return $definition;
48
    }
49
50
    /**
51
     * @param array                $fieldDefinition
52
     * @param FieldModel           $field
53
     * @param string               $fieldHandle
54
     * @param FieldGroupModel|null $group
55
     */
56
    public function populate(array $fieldDefinition, FieldModel $field, $fieldHandle, FieldGroupModel $group = null)
57
    {
58
        parent::populate($fieldDefinition, $field, $fieldHandle, $group);
59
60
        $settings = $fieldDefinition['settings'];
61
62 View Code Duplication
        if (array_key_exists('defaultUploadLocationSource', $settings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $defaultUploadLocationSourceId = $settings['defaultUploadLocationSource'];
64
            $defaultUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeByHandle($defaultUploadLocationSourceId);
65
            $settings['defaultUploadLocationSource'] = $defaultUploadLocationSource ? $defaultUploadLocationSource->id : '';
66
        }
67
68 View Code Duplication
        if (array_key_exists('singleUploadLocationSource', $settings)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            $singleUploadLocationSourceId = $settings['singleUploadLocationSource'];
70
            $singleUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeByHandle($singleUploadLocationSourceId);
71
            $settings['singleUploadLocationSource'] = $singleUploadLocationSource ? $singleUploadLocationSource->id : '';
72
        }
73
74
        $field->settings = $settings;
75
    }
76
}
77