FieldsSeeder::createFieldValues()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 3
1
<?php
2
3
use Carbon\Carbon;
4
use Illuminate\Database\Seeder;
5
use Illuminate\Support\Facades\DB;
6
7
class FieldsSeeder extends Seeder
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * @var \Carbon\Carbon
11
     */
12
    protected $now;
13
14
    /**
15
     * @var array
16
     */
17
    protected $fields = [
18
        [
19
            'id' => 1,
20
            'name' => 'Operating System',
21
            'values' => ['iOS', 'Android', 'Windows Phone'], // 1, 2, 3
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22
        ],
23
        [
24
            'id' => 2,
25
            'name' => 'RAM Size',
26
            'values' => ['512MB', '1GB', '2GB'], // 4, 5, 6
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
27
        ],
28
        [
29
            'id' => 3,
30
            'name' => 'Storage Size',
31
            'values' => ['16GB', '32GB', '64GB', '128GB', '256GB'], // 7, 8, 9, 10, 11
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
32
        ],
33
        [
34
            'id' => 4,
35
            'name' => 'Screen Resolution',
36
            'values' => ['375x667', '414x736', '960x540', '1136x640', '1280x768', '1280x720', '1334x750', '1920x1080'], // 12, 13, 14, 15, 16, 17, 18, 19
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
37
        ],
38
        [
39
            'id' => 5,
40
            'name' => 'CPU Cores Count',
41
            'values' => ['1', '2', '4', '6', '8'], // 20, 21, 22, 23, 24
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
        ],
43
    ];
44
45
    /**
46
     * Run the fields seeder.
47
     */
48
    public function run()
49
    {
50
        $this->now = Carbon::now();
51
52
        $fieldValueId = 1;
53
54
        foreach ($this->fields as $field) {
55
            DB::table('fields')->insert($this->createField($field['id'], $field['name']));
56
57
            DB::table('field_values')->insert(
58
                $this->createFieldValues($field['id'], $fieldValueId, $field['values'])
59
            );
60
61
            $fieldValueId += count($field['values']);
62
        }
63
    }
64
65
    /**
66
     * @param int $id
67
     * @param string $name
68
     * @return array
69
     */
70 View Code Duplication
    protected function createField(int $id, string $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
71
    {
72
        return [
73
            'id' => $id,
74
            'name' => $name,
75
            'created_at' => $this->now,
76
            'updated_at' => $this->now,
77
        ];
78
    }
79
80
    /**
81
     * @param int $fieldId
82
     * @param int $fieldValueId
83
     * @param array $values
84
     * @return array
85
     */
86
    protected function createFieldValues(int $fieldId, int $fieldValueId, array $values)
87
    {
88
        $fieldValues = [];
89
90
        foreach ($values as $value) {
91
            $fieldValues[] = [
92
                'id' => $fieldValueId,
93
                'field_id' => $fieldId,
94
                'value' => $value,
95
                'created_at' => $this->now,
96
                'updated_at' => $this->now,
97
            ];
98
99
            ++$fieldValueId;
100
        }
101
102
        return $fieldValues;
103
    }
104
}
105