GetValueFieldType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 75
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 47 4
1
<?php namespace Anomaly\PreferencesModule\Preference\Command;
2
3
use Anomaly\PreferencesModule\Preference\Contract\PreferenceInterface;
4
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
5
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypeCollection;
6
use Illuminate\Contracts\Config\Repository;
7
8
/**
9
 * Class GetValueFieldType
10
 *
11
 * @link          http://pyrocms.com/
12
 * @author        PyroCMS, Inc. <[email protected]>
13
 * @author        Ryan Thompson <[email protected]>
14
 */
15
class GetValueFieldType
16
{
17
18
    /**
19
     * The preference instance.
20
     *
21
     * @var PreferenceInterface
22
     */
23
    protected $preference;
24
25
    /**
26
     * Create a new GetValueFieldType instance.
27
     *
28
     * @param PreferenceInterface $preference
29
     */
30
    public function __construct(PreferenceInterface $preference)
31
    {
32
        $this->preference = $preference;
33
    }
34
35
    /**
36
     * Handle the command.
37
     *
38
     * @param  FieldTypeCollection $fieldTypes
39
     * @param  Repository          $config
40
     * @return FieldType
41
     */
42
    public function handle(FieldTypeCollection $fieldTypes, Repository $config)
43
    {
44
        // Get the preference's key.
45
        $key = $this->preference->getKey();
46
47
        // Get the bare value.
48
        $value = array_get($this->preference->getAttributes(), 'value');
49
50
        // Try and find the preference's field configuration.
51
        if (!$field = $config->get(str_replace('::', '::preferences/preferences.', $key))) {
52
            $field = $config->get(str_replace('::', '::preferences.', $key));
53
        }
54
55
        // Convert short syntax.
56
        if (is_string($field)) {
57
            $field = [
58
                'type' => $field,
59
            ];
60
        }
61
62
        /*
63
         * Try and get the field type that
64
         * the preference uses. If none exists
65
         * then just return the value as is.
66
         */
67
        $type = $fieldTypes->get(array_get($field, 'type'));
68
69
        if (!$type) {
70
            return null;
71
        }
72
73
        // Setup the field type.
74
        $type->setEntry($this->preference);
75
        $type->mergeRules(array_get($field, 'rules', []));
76
        $type->mergeConfig(array_get($field, 'config', []));
77
78
        /*
79
         * If the type can be determined then
80
         * get the modifier and restore the value
81
         * before returning it.
82
         */
83
        $modifier = $type->getModifier();
84
85
        $type->setValue($modifier->restore($value));
86
87
        return $type;
88
    }
89
}
90