ArticlesController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use Yajra\CMS\DataTables\ArticlesDataTable;
6
use Yajra\CMS\Entities\Article;
7
use Yajra\CMS\Events\Articles\ArticleWasCreated;
8
use Yajra\CMS\Events\Articles\ArticleWasUpdated;
9
use Yajra\CMS\Http\Requests\ArticlesFormRequest;
10
11
class ArticlesController extends Controller
12
{
13
    /**
14
     * Controller specific permission ability map.
15
     *
16
     * @var array
17
     */
18
    protected $customPermissionMap = [
19
        'publish' => 'update',
20
    ];
21
22
    /**
23
     * ArticlesController constructor.
24
     */
25
    public function __construct()
26
    {
27
        $this->authorizePermissionResource('article');
28
    }
29
30
    /**
31
     * Display list of articles.
32
     *
33
     * @param \Yajra\CMS\DataTables\ArticlesDataTable $dataTable
34
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
35
     */
36
    public function index(ArticlesDataTable $dataTable)
37
    {
38
        return $dataTable->render('administrator.articles.index');
39
    }
40
41
    /**
42
     * Show articles form.
43
     *
44
     * @param \Yajra\CMS\Entities\Article $article
45
     * @return mixed
46
     */
47 View Code Duplication
    public function create(Article $article)
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...
48
    {
49
        $article->published = true;
50
        $article->setHighestOrderNumber();
51
        $tags = Article::existingTags()->pluck('name');
52
53
        return view('administrator.articles.create', compact('article', 'tags'));
54
    }
55
56
    /**
57
     * Store a newly created article.
58
     *
59
     * @param ArticlesFormRequest $request
60
     * @return mixed
61
     */
62 View Code Duplication
    public function store(ArticlesFormRequest $request)
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...
63
    {
64
        $article = new Article;
65
        $article->fill($request->all());
66
        $article->published     = $request->get('published', false);
67
        $article->featured      = $request->get('featured', false);
68
        $article->authenticated = $request->get('authenticated', false);
69
        $article->is_page       = $request->get('is_page', false);
70
        $article->save();
71
72
        $article->permissions()->sync($request->get('permissions', []));
73
        if ($request->tags) {
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Yajra\CMS\Http\Re...ts\ArticlesFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
            $article->tag(explode(',', $request->tags));
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Yajra\CMS\Http\Re...ts\ArticlesFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
        }
76
77
        event(new ArticleWasCreated($article));
78
79
        flash()->success(trans('cms::article.store.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::article.store.success') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80
81
        return redirect()->route('administrator.articles.index');
82
    }
83
84
    /**
85
     * Show and edit selected article.
86
     *
87
     * @param \Yajra\CMS\Entities\Article $article
88
     * @return mixed
89
     */
90 View Code Duplication
    public function edit(Article $article)
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...
91
    {
92
        $tags         = Article::existingTags()->pluck('name');
93
        $selectedTags = implode(',', $article->tagNames());
94
95
        return view('administrator.articles.edit', compact('article', 'tags', 'selectedTags'));
96
    }
97
98
    /**
99
     * Update selected article.
100
     *
101
     * @param \Yajra\CMS\Entities\Article $article
102
     * @param ArticlesFormRequest $request
103
     * @return mixed
104
     */
105 View Code Duplication
    public function update(Article $article, ArticlesFormRequest $request)
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...
106
    {
107
        $article->fill($request->all());
108
        $article->published     = $request->get('published', false);
109
        $article->featured      = $request->get('featured', false);
110
        $article->authenticated = $request->get('authenticated', false);
111
        $article->is_page       = $request->get('is_page', false);
112
        $article->save();
113
114
        $article->permissions()->sync($request->get('permissions', []));
115
        if ($request->tags) {
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Yajra\CMS\Http\Re...ts\ArticlesFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
116
            $article->retag(explode(',', $request->tags));
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Yajra\CMS\Http\Re...ts\ArticlesFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
        }
118
119
        event(new ArticleWasUpdated($article));
120
121
        flash()->success(trans('cms::article.update.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::article.update.success') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
122
123
        return redirect()->route('administrator.articles.index');
124
    }
125
126
    /**
127
     * Remove selected article.
128
     *
129
     * @param \Yajra\CMS\Entities\Article $article
130
     * @return \Illuminate\Http\JsonResponse
131
     * @throws \Exception
132
     */
133
    public function destroy(Article $article)
134
    {
135
        $article->delete();
136
137
        return $this->notifySuccess(trans('cms::article.destroy.success'));
138
    }
139
140
    /**
141
     * Publish/Unpublish a article.
142
     *
143
     * @param \Yajra\CMS\Entities\Article $article
144
     * @return \Illuminate\Http\JsonResponse
145
     */
146
    public function publish(Article $article)
147
    {
148
        $article->togglePublishedState();
149
150
        return $this->notifySuccess(trans(
151
                'cms::article.update.publish', [
152
                'task' => $article->published ? 'published' : 'unpublished',
153
            ])
154
        );
155
    }
156
}
157