Issues (97)

app/Services/PostFormFields.php (4 issues)

1
<?php
2
3
namespace App\Services;
4
5
use App\Models\Post;
6
use App\Models\Tag;
7
use Carbon\Carbon;
8
9
class PostFormFields
10
{
11
    /**
12
     * List of fields and default value for each field.
13
     *
14
     * @var array
15
     */
16
    protected $fieldList = [
17
        'title'             => '',
18
        'subtitle'          => '',
19
        'post_image'        => '',
20
        'content'           => '',
21
        'meta_description'  => '',
22
        'is_draft'          => '1',
23
        'author'            => '',
24
        'slug'              => '',
25
        'publish_date'      => '',
26
        'publish_time'      => '',
27
        'layout'            => 'blog.post',
28
        'tags'              => [],
29
    ];
30
31
    /**
32
     * Create a new job instance.
33
     *
34
     * @param int $id
35
     *
36
     * @return void
37
     */
38
    public function __construct($id = null)
39
    {
40
        $this->id = $id;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
    }
42
43
    /**
44
     * Execute the job.
45
     *
46
     * @return void
47
     */
48
    public function handle()
49
    {
50
        $fields = $this->fieldList;
51
52
        if ($this->id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
53
            $fields = $this->fieldsFromModel($this->id, $fields);
54
            $fields['publish_time'] = $fields['publish_date']->format('g:i A');
55
            $fields['publish_date'] = $fields['publish_date']->format('M-d-Y');
56
        } else {
57
            $when = Carbon::now()->addHour();
58
            $fields['publish_date'] = $when->format('M-j-Y');
59
            $fields['publish_time'] = $when->format('g:i A');
60
        }
61
62
        foreach ($fields as $fieldName => $fieldValue) {
63
            $fields[$fieldName] = old($fieldName, $fieldValue);
64
        }
65
66
        // Get the additional data for the form fields
67
        $postFormFieldData = $this->postFormFieldData();
68
69
        return array_merge(
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_merge($fiel...)), $postFormFieldData) returns the type array which is incompatible with the documented return type void.
Loading history...
70
            $fields,
71
            [
72
                'allTags' => Tag::pluck('tag')->all(),
73
            ],
74
            $postFormFieldData
75
        );
76
    }
77
78
    /**
79
     * Return the field values from the model.
80
     *
81
     * @param int   $id
82
     * @param array $fields
83
     *
84
     * @return array
85
     */
86
    protected function fieldsFromModel($id, array $fields)
87
    {
88
        $page = Post::findOrFail($id);
89
90
        $fieldNames = array_keys(array_except($fields, ['tags']));
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated: Arr::except() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

90
        $fieldNames = array_keys(/** @scrutinizer ignore-deprecated */ array_except($fields, ['tags']));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
91
92
        $fields = [
93
            'id' => $id,
94
        ];
95
        foreach ($fieldNames as $field) {
96
            $fields[$field] = $page->{$field};
97
        }
98
99
        $fields['tags'] = $page->tags()->pluck('tag')->all();
100
101
        return $fields;
102
    }
103
104
    /**
105
     * Get the additonal post form fields data.
106
     *
107
     * @return array
108
     */
109
    protected function postFormFieldData()
110
    {
111
        $allAvailableAuthors = PostAuthors::all();
112
        $postTemplates = PostTemplates::list();
113
114
        return [
115
            'allAvailableAuthors'   => $allAvailableAuthors,
116
            'postTemplates'         => $postTemplates,
117
        ];
118
    }
119
}
120