Completed
Push — master ( 98a062...441031 )
by Ryan
10:10
created

EloquentFormRepository::save()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Model;
2
3
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
4
use Anomaly\Streams\Platform\Ui\Form\Contract\FormRepositoryInterface;
5
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
6
7
/**
8
 * Class EloquentFormRepository
9
 *
10
 * @link   http://pyrocms.com/
11
 * @author PyroCMS, Inc. <[email protected]>
12
 * @author Ryan Thompson <[email protected]>
13
 */
14
class EloquentFormRepository implements FormRepositoryInterface
15
{
16
17
    /**
18
     * The form model.
19
     *
20
     * @var EloquentModel
21
     */
22
    protected $model;
23
24
    /**
25
     * Create a new EloquentFormRepositoryInterface instance.
26
     *
27
     * @param EloquentModel $model
28
     */
29
    public function __construct(EloquentModel $model)
30
    {
31
        $this->model = $model;
32
    }
33
34
    /**
35
     * Find an entry.
36
     *
37
     * @param $id
38
     * @return EloquentModel
39
     */
40
    public function findOrNew($id)
41
    {
42
        return $this->model->findOrNew($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrNew does not exist on object<Anomaly\Streams\P...rm\Model\EloquentModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
43
    }
44
45
    /**
46
     * Save the form.
47
     *
48
     * @param FormBuilder $builder
49
     */
50
    public function save(FormBuilder $builder)
51
    {
52
        $entry = $builder->getFormEntry();
53
54
        $data = $this->prepareValueData($builder);
55
56
        $entry->unguard();
0 ignored issues
show
Bug introduced by
The method unguard does only exist in Anomaly\Streams\Platform\Model\EloquentModel, but not in Anomaly\Streams\Platform...Contract\EntryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
58
        $builder->fire('querying', compact('builder'));
59
60
        /**
61
         * Update OR create the entry.
62
         * Keep this as is or we will
63
         * have issues with post relations
64
         * in following observer logic.
65
         */
66
        if ($entry->getId()) {
67
            $entry->update($data);
0 ignored issues
show
Bug introduced by
The method update does only exist in Anomaly\Streams\Platform\Model\EloquentModel, but not in Anomaly\Streams\Platform...Contract\EntryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
68
        } else {
69
            $entry = $entry->create($data);
0 ignored issues
show
Bug introduced by
The method create does only exist in Anomaly\Streams\Platform\Model\EloquentModel, but not in Anomaly\Streams\Platform...Contract\EntryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
        }
71
72
        $entry->reguard();
73
74
        $builder->setFormEntry($entry);
75
76
        $this->processSelfHandlingFields($builder);
77
    }
78
79
    /**
80
     * Prepare the value data for update / create.
81
     *
82
     * @param  FormBuilder $builder
83
     * @return array
84
     */
85
    protected function prepareValueData(FormBuilder $builder)
86
    {
87
        $form = $builder->getForm();
88
89
        $entry  = $form->getEntry();
90
        $fields = $form->getFields();
91
92
        $allowed  = $fields->allowed();
93
        $disabled = $fields->disabled();
94
95
        /*
96
         * Set initial data from the
97
         * entry, minus undesired data.
98
         */
99
        $data = array_diff_key(
100
            $entry->getUnguardedAttributes(),
0 ignored issues
show
Bug introduced by
The method getUnguardedAttributes does only exist in Anomaly\Streams\Platform\Model\EloquentModel, but not in Anomaly\Streams\Platform...Contract\EntryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
101
            array_merge(
102
                ['id', 'created_at', 'created_by_id', 'updated_at', 'updated_by_id'],
103
                array_flip($disabled->fieldSlugs())
104
            )
105
        );
106
107
        /**
108
         * Save default translation input.
109
         *
110
         * @var FieldType $field
111
         */
112
        foreach ($allowed->notTranslatable() as $field) {
113
            if (!$field->getLocale()) {
114
                array_set($data, str_replace('__', '.', $field->getField()), $form->getValue($field->getInputName()));
115
            }
116
        }
117
118
        /*
119
         * Loop through available translations
120
         * and save translated input.
121
         *
122
         * @var FieldType $field
123
         */
124
        if ($entry->getTranslationModelName()) {
0 ignored issues
show
Bug introduced by
The method getTranslationModelName does only exist in Anomaly\Streams\Platform\Model\EloquentModel, but not in Anomaly\Streams\Platform...Contract\EntryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
125
            foreach (config('streams::locales.enabled') as $locale) {
126
                foreach ($allowed->translatable() as $field) {
127
                    if ($field->getLocale() == $locale) {
128
                        array_set(
129
                            $data,
130
                            $locale . '.' . $field->getField(),
131
                            $form->getValue($field->getInputName())
132
                        );
133
                    }
134
                }
135
            }
136
        }
137
138
        return $data;
139
    }
140
141
    /**
142
     * Process fields that handle themselves.
143
     *
144
     * @param FormBuilder $builder
145
     */
146
    protected function processSelfHandlingFields(FormBuilder $builder)
147
    {
148
        $form = $builder->getForm();
149
150
        $entry  = $form->getEntry();
151
        $fields = $form->getFields();
152
153
        $fields = $fields->selfHandling();
154
155
        /* @var FieldType $field */
156
        foreach ($fields as $field) {
157
            app()->call([$field->setEntry($entry), 'handle'], compact('builder'));
158
        }
159
    }
160
}
161