ConfigurationFormRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
rs 9.6666
1
<?php namespace Anomaly\ConfigurationModule\Configuration\Form;
2
3
use Anomaly\ConfigurationModule\Configuration\Contract\ConfigurationRepositoryInterface;
4
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
5
use Anomaly\Streams\Platform\Ui\Form\Contract\FormRepositoryInterface;
6
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
7
use Illuminate\Config\Repository;
8
use Illuminate\Container\Container;
9
10
/**
11
 * Class ConfigurationFormRepositoryInterface
12
 *
13
 * @link          http://pyrocms.com/
14
 * @author        PyroCMS, Inc. <[email protected]>
15
 * @author        Ryan Thompson <[email protected]>
16
 */
17
class ConfigurationFormRepository implements FormRepositoryInterface
18
{
19
20
    /**
21
     * The config repository.
22
     *
23
     * @var Repository
24
     */
25
    protected $config;
26
27
    /**
28
     * The application container.
29
     *
30
     * @var Container
31
     */
32
    protected $container;
33
34
    /**
35
     * The configurations repository.
36
     *
37
     * @var ConfigurationRepositoryInterface
38
     */
39
    protected $configurations;
40
41
    /**
42
     * Create a new ConfigurationFormRepositoryInterface instance.
43
     *
44
     * @param Repository                       $config
45
     * @param Container                        $container
46
     * @param ConfigurationRepositoryInterface $configurations
47
     */
48
    public function __construct(
49
        Repository $config,
50
        Container $container,
51
        ConfigurationRepositoryInterface $configurations
52
    ) {
53
        $this->config         = $config;
54
        $this->configurations = $configurations;
55
        $this->container      = $container;
56
    }
57
58
    /**
59
     * Find an entry or return a new one.
60
     *
61
     * @param $id
62
     * @return string
63
     */
64
    public function findOrNew($id)
65
    {
66
        return $id;
67
    }
68
69
    /**
70
     * Save the form.
71
     *
72
     * @param  FormBuilder|ConfigurationFormBuilder $builder
73
     * @return bool|mixed
74
     */
75
    public function save(FormBuilder $builder)
76
    {
77
        $namespace = $builder->getFormEntry() . '::';
78
79
        /* @var FieldType $field */
80
        foreach ($builder->getFormFields() as $field) {
81
82
            $scope = $builder->getScope();
83
            $key   = $namespace . $field->getField();
84
            $value = $builder->getFormValue($field->getInputName());
85
86
            $this->configurations->set($key, $scope, $value);
87
        }
88
    }
89
}
90