SearchController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 29
c 4
b 0
f 0
dl 0
loc 68
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFactFinderSearchRequestTransfer() 0 8 1
A redirect() 0 11 5
A indexAction() 0 27 5
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\FactFinder\Controller;
9
10
use Generated\Shared\Transfer\FactFinderSdkSearchRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...dkSearchRequestTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\FactFinderSdkSearchResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...kSearchResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Spryker\Shared\Kernel\Store;
13
use Spryker\Yves\Kernel\Controller\AbstractController;
14
use SprykerEco\Shared\FactFinder\FactFinderConstants;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * @method \SprykerEco\Yves\FactFinder\FactFinderFactory getFactory()
19
 */
20
class SearchController extends AbstractController
21
{
22
    /**
23
     * @param \Symfony\Component\HttpFoundation\Request $request
24
     *
25
     * @return array
26
     */
27
    public function indexAction(Request $request)
28
    {
29
        $factFinderSearchRequestTransfer = $this->createFactFinderSearchRequestTransfer($request);
30
31
        $ffSearchResponseTransfer = $this->getFactory()
32
            ->getFactFinderClient()
33
            ->search($factFinderSearchRequestTransfer);
34
35
        $redirectUrl = $this->redirect($ffSearchResponseTransfer);
36
        if ($redirectUrl !== null) {
37
            return $this->redirectResponseExternal($redirectUrl);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->redirectRe...eExternal($redirectUrl) returns the type Symfony\Component\HttpFoundation\RedirectResponse which is incompatible with the documented return type array.
Loading history...
38
        }
39
40
        $feedbackForm = $this->getFactory()
41
            ->createFeedbackForm();
42
43
        if (!$ffSearchResponseTransfer->getResult()) {
44
            $this->addErrorMessage('Search is not available at the moment');
45
        }
46
47
        return [
48
            'searchResponse' => $ffSearchResponseTransfer,
49
            'pagingRote' => 'fact-finder',
50
            'lang' => Store::getInstance()->getCurrentLanguage(),
51
            'query' => isset($requestArray['query']) ? $requestArray['query'] : '',
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $requestArray does not exist. Did you maybe mean $request?
Loading history...
52
            'page' => isset($requestArray['page']) ? $requestArray['page'] : '',
53
            'feedbackForm' => $feedbackForm->createView(),
0 ignored issues
show
Bug introduced by
The method createView() does not exist on SprykerEco\Yves\FactFind...earchResultFeedbackForm. ( Ignorable by Annotation )

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

53
            'feedbackForm' => $feedbackForm->/** @scrutinizer ignore-call */ createView(),

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...
54
        ];
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\FactFinderSdkSearchResponseTransfer $factFinderSdkSearchResponseTransfer
59
     *
60
     * @return null|string
61
     */
62
    protected function redirect(FactFinderSdkSearchResponseTransfer $factFinderSdkSearchResponseTransfer)
63
    {
64
        if ($factFinderSdkSearchResponseTransfer->getCampaignIterator() !== null && $factFinderSdkSearchResponseTransfer->getCampaignIterator()->getHasRedirect()) {
65
            return $factFinderSdkSearchResponseTransfer->getCampaignIterator()->getRedirectUrl();
66
        }
67
68
        if ($factFinderSdkSearchResponseTransfer->getSearchRedirect() !== null && $factFinderSdkSearchResponseTransfer->getSearchRedirect()->getRedirect() === true) {
69
            return $factFinderSdkSearchResponseTransfer->getSearchRedirect()->getUrl();
70
        }
71
72
        return null;
73
    }
74
75
    /**
76
     * @param \Symfony\Component\HttpFoundation\Request $request
77
     *
78
     * @return \Generated\Shared\Transfer\FactFinderSdkSearchRequestTransfer
79
     */
80
    protected function createFactFinderSearchRequestTransfer(Request $request)
81
    {
82
        $factFinderSearchRequestTransfer = new FactFinderSdkSearchRequestTransfer();
83
        $requestArray = $request->query->all();
84
        $requestArray[FactFinderConstants::REQUEST_PARAMETER_SID] = $request->cookies->get(FactFinderConstants::COOKIE_SID_NAME);
85
        $factFinderSearchRequestTransfer->setRequest($requestArray);
86
87
        return $factFinderSearchRequestTransfer;
88
    }
89
}
90