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

AssignmentsController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Http\Controller;
2
3
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection;
4
use Anomaly\Streams\Platform\Assignment\Form\AssignmentFormBuilder;
5
use Anomaly\Streams\Platform\Assignment\Table\AssignmentTableBuilder;
6
use Anomaly\Streams\Platform\Field\Contract\FieldInterface;
7
use Anomaly\Streams\Platform\Field\Contract\FieldRepositoryInterface;
8
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
9
use Anomaly\Streams\Platform\Stream\Contract\StreamRepositoryInterface;
10
11
/**
12
 * Class AssignmentsController
13
 *
14
 * @link   http://pyrocms.com/
15
 * @author PyroCMS, Inc. <[email protected]>
16
 * @author Ryan Thompson <[email protected]>
17
 */
18
class AssignmentsController extends AdminController
19
{
20
21
    /**
22
     * The stream namespace.
23
     *
24
     * @var null|string
25
     */
26
    protected $namespace = null;
27
28
    /**
29
     * Return an index of existing assignments.
30
     *
31
     * @param  AssignmentTableBuilder    $table
32
     * @param  StreamRepositoryInterface $streams
33
     * @return \Symfony\Component\HttpFoundation\Response
34
     */
35 View Code Duplication
    public function index(AssignmentTableBuilder $table, StreamRepositoryInterface $streams)
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...
36
    {
37
        /* @var StreamInterface $stream */
38
        if (!$stream = $streams->find($this->route->parameter('stream'))) {
39
            $stream = $streams->findBySlugAndNamespace(
40
                $this->route->parameter('stream'),
41
                $this->getNamespace()
42
            );
43
        }
44
45
        return $table
46
            ->setStream($stream)
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type null or object<Anomaly\Streams\P...rm\Model\EloquentModel>; however, Anomaly\Streams\Platform...bleBuilder::setStream() does only seem to accept object<Anomaly\Streams\P...ntract\StreamInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47
            ->render();
48
    }
49
50
    /**
51
     * Choose a field to assign.
52
     *
53
     * @param  FieldRepositoryInterface  $fields
54
     * @param  StreamRepositoryInterface $streams
55
     * @param ModuleCollection           $modules
56
     * @return \Illuminate\Contracts\View\View|mixed
57
     */
58
    public function choose(
59
        FieldRepositoryInterface $fields,
60
        StreamRepositoryInterface $streams,
61
        ModuleCollection $modules
62
    ) {
63
        $module = $modules->active();
64
65
        /* @var StreamInterface $stream */
66
        if (!$stream = $streams->find($this->route->parameter('stream'))) {
67
            $stream = $streams->findBySlugAndNamespace($this->route->parameter('stream'), $this->getNamespace());
68
        }
69
70
        $fields = $fields
71
            ->findAllByNamespace($this->getNamespace())
72
            ->notAssignedTo($stream)
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type null or object<Anomaly\Streams\P...rm\Model\EloquentModel>; however, Anomaly\Streams\Platform...ection::notAssignedTo() does only seem to accept object<Anomaly\Streams\P...ntract\StreamInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
73
            ->unlocked();
74
75
        return $this->view->make('streams::assignments/choose', compact('fields', 'group', 'module'));
76
    }
77
78
    /**
79
     * Create a new assignment.
80
     *
81
     * @param  AssignmentFormBuilder     $builder
82
     * @param  StreamRepositoryInterface $streams
83
     * @param  FieldRepositoryInterface  $fields
84
     * @return \Symfony\Component\HttpFoundation\Response
85
     */
86
    public function create(
87
        AssignmentFormBuilder $builder,
88
        StreamRepositoryInterface $streams,
89
        FieldRepositoryInterface $fields
90
    ) {
91
        /* @var StreamInterface $stream */
92
        if (!$stream = $streams->find($this->route->parameter('stream'))) {
93
            $stream = $streams->findBySlugAndNamespace($this->route->parameter('stream'), $this->getNamespace());
94
        }
95
96
        /* @var FieldInterface $field */
97
        $field = $fields->find($this->request->get('field'));
98
99
        return $builder
100
            ->setField($field)
101
            ->setStream($stream)
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type null or object<Anomaly\Streams\P...rm\Model\EloquentModel>; however, Anomaly\Streams\Platform...ormBuilder::setStream() does only seem to accept object<Anomaly\Streams\P...ntract\StreamInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
102
            ->render();
103
    }
104
105
    /**
106
     * Edit an existing assignment.
107
     *
108
     * @param  AssignmentFormBuilder     $builder
109
     * @param  StreamRepositoryInterface $streams
110
     * @return \Symfony\Component\HttpFoundation\Response
111
     */
112 View Code Duplication
    public function edit(AssignmentFormBuilder $builder, StreamRepositoryInterface $streams)
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...
113
    {
114
        /* @var StreamInterface $stream */
115
        if (!$stream = $streams->find($this->route->parameter('stream'))) {
116
            $stream = $streams->findBySlugAndNamespace($this->route->parameter('stream'), $this->getNamespace());
117
        }
118
119
        return $builder
120
            ->setStream($stream)
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type null or object<Anomaly\Streams\P...rm\Model\EloquentModel>; however, Anomaly\Streams\Platform...ormBuilder::setStream() does only seem to accept object<Anomaly\Streams\P...ntract\StreamInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
121
            ->render($this->route->parameter('id'));
122
    }
123
124
    /**
125
     * Get the namespace.
126
     *
127
     * @return null|string
128
     */
129
    public function getNamespace()
130
    {
131
        return $this->namespace;
132
    }
133
}
134