Completed
Push — master ( 01228a...602dee )
by
06:56
created

ResourceController::findOr404()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace DoS\ResourceBundle\Controller;
4
5
use FOS\RestBundle\View\View;
6
use Sylius\Bundle\ResourceBundle\Controller\ResourceController as BaseResourceController;
7
use Sylius\Component\Resource\ResourceActions;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
use Symfony\Component\PropertyAccess\PropertyAccess;
13
14
class ResourceController extends BaseResourceController
15
{
16
    protected $activedPath = 'actived';
17
    protected $enabledPath = 'enabled';
18
19
    /**
20
     * @param $string
21
     *
22
     * @return bool
23
     */
24
    protected function stringToBoolean($string)
25
    {
26
        $string = is_string($string) ? strtolower($string) : $string;
27
28
        if (in_array($string, array(true, 1, '1', 'yes', 'true'), true)) {
29
            return true;
30
        }
31
32
        return false;
33
    }
34
35
    /**
36
     * @param Request $request
37
     * @param $state
38
     * @param null $path
39
     *
40
     * @return RedirectResponse|Response
41
     */
42
    public function activeStateAction(Request $request, $state, $path = null)
43
    {
44
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
0 ignored issues
show
Bug introduced by
The property requestConfigurationFactory does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property metadata does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
        $path = $path ?: $this->activedPath;
46
        $resource = $this->findOr404($configuration);
47
        $accessor = PropertyAccess::createPropertyAccessor();
48
        $accessor->setValue($resource, $path, $this->stringToBoolean($state));
49
50
        if ($state) {
51
            // reset other to false
52
            $this->repository->bulkUpdate(array($path => false));
0 ignored issues
show
Bug introduced by
The property repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
            $this->manager->flush();
0 ignored issues
show
Bug introduced by
The property manager does not seem to exist. Did you mean domainManager?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
        }
55
56
        if (!$configuration->isHtmlRequest()) {
57
            return $this->viewHandler->handle($configuration, View::create($resource, 204));
0 ignored issues
show
Bug introduced by
The property viewHandler does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
        }
59
60
        return $this->redirectHandler->redirectToReferer($configuration);
0 ignored issues
show
Unused Code introduced by
The call to RedirectHandler::redirectToReferer() has too many arguments starting with $configuration.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
    }
62
63
    /**
64
     * @param Request $request
65
     * @param $state
66
     * @param null $path
67
     *
68
     * @return RedirectResponse|Response
69
     */
70
    public function enableStateAction(Request $request, $state, $path = null)
71
    {
72
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
0 ignored issues
show
Bug introduced by
The property requestConfigurationFactory does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
74
        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
0 ignored issues
show
Unused Code introduced by
The call to ResourceController::isGrantedOr403() has too many arguments starting with \Sylius\Component\Resource\ResourceActions::UPDATE.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
        $resource = $this->findOr404($configuration);
76
77
        $path = $path ?: $this->enabledPath;
78
        $accessor = PropertyAccess::createPropertyAccessor();
79
        $accessor->setValue($resource, $path, $this->stringToBoolean($state));
80
81
        $resource = $this->findOr404($configuration);
82
83
        $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource);
0 ignored issues
show
Bug introduced by
The property eventDispatcher does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
84
        $this->manager->flush();
0 ignored issues
show
Bug introduced by
The property manager does not seem to exist. Did you mean domainManager?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
85
        $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);
86
87
        if (!$configuration->isHtmlRequest()) {
88
            return $this->viewHandler->handle($configuration, View::create($resource, 204));
89
        }
90
91
        $this->flashHelper->addSuccessFlash($configuration, $state ? $path . '_enabled' : $path . '_disabled', $resource);
0 ignored issues
show
Bug introduced by
The method addSuccessFlash() does not seem to exist on object<Sylius\Bundle\Res...Controller\FlashHelper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
93
        return $this->redirectHandler->redirectToIndex($configuration, $resource);
0 ignored issues
show
Unused Code introduced by
The call to RedirectHandler::redirectToIndex() has too many arguments starting with $configuration.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
94
    }
95
96
    public function batchDeleteAction(Request $request, array $ids = null)
97
    {
98
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
0 ignored issues
show
Bug introduced by
The property requestConfigurationFactory does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
99
100
        $this->isGrantedOr403($configuration, ResourceActions::DELETE);
0 ignored issues
show
Unused Code introduced by
The call to ResourceController::isGrantedOr403() has too many arguments starting with \Sylius\Component\Resource\ResourceActions::DELETE.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101
102
        if (null == $ids) {
103
            $ids = $request->get('ids');
104
        }
105
106
        if (is_string($ids)) {
107
            $ids = explode( ',', $ids);
108
        }
109
110
        $resources = $this->repository->findBy(array(
111
            'id' => $ids
112
        ));
113
114
        if (empty($resources)) {
115
            throw new NotFoundHttpException(
116
                sprintf(
117
                    'Requested %s does not exist with these ids: %s.',
118
                    $this->metadata->getPluralName(),
119
                    json_encode($configuration->getCriteria($ids))
120
                )
121
            );
122
        }
123
124
        foreach ($resources as $resource) {
125
            $this->manager->remove($resource);
0 ignored issues
show
Bug introduced by
The property manager does not seem to exist. Did you mean domainManager?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
126
        }
127
128
        $this->manager->flush();
0 ignored issues
show
Bug introduced by
The property manager does not seem to exist. Did you mean domainManager?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
129
130
        if (!$configuration->isHtmlRequest()) {
131
            return $this->viewHandler->handle($configuration, View::create(null, 204));
132
        }
133
134
        return $this->redirectHandler->redirectToIndex($configuration);
0 ignored issues
show
Unused Code introduced by
The call to RedirectHandler::redirectToIndex() has too many arguments starting with $configuration.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
135
    }
136
}
137