Passed
Pull Request — master (#23)
by Volodymyr
48:37
created

IndexController::getSecurityInvoiceAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
c 0
b 0
f 0
rs 9.52
cc 3
nc 3
nop 1
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\Payone\Controller;
9
10
use Generated\Shared\Transfer\PayoneCancelRedirectTransfer;
11
use Generated\Shared\Transfer\PayoneGetFileTransfer;
12
use Generated\Shared\Transfer\PayoneGetInvoiceTransfer;
13
use Generated\Shared\Transfer\PayoneTransactionStatusUpdateTransfer;
14
use Spryker\Yves\Kernel\Controller\AbstractController;
15
use SprykerEco\Yves\Payone\Plugin\Provider\PayoneControllerProvider;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\StreamedResponse;
18
19
/**
20
 * @method \SprykerEco\Client\Payone\PayoneClientInterface getClient()
21
 * @method \SprykerEco\Yves\Payone\PayoneFactory getFactory()
22
 */
23
class IndexController extends AbstractController
24
{
25
    /**
26
     * @param \Symfony\Component\HttpFoundation\Request $request
27
     *
28
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
29
     */
30
    public function indexAction(Request $request)
31
    {
32
        $statusUpdateTranfer = new PayoneTransactionStatusUpdateTransfer();
33
        $statusUpdateTranfer->fromArray($request->request->all(), true);
34
35
        $response = $this->getClient()->updateStatus($statusUpdateTranfer)->getResponse();
36
37
        $callback = function () use ($response) {
38
            echo $response;
39
        };
40
41
        return $this->streamedResponse($callback);
42
    }
43
44
    /**
45
     * @param \Symfony\Component\HttpFoundation\Request $request
46
     *
47
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
48
     */
49 View Code Duplication
    public function getFileAction(Request $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...
50
    {
51
        $customerClient = $this->getFactory()->createCustomerClient();
0 ignored issues
show
Bug introduced by
The method createCustomerClient() does not seem to exist on object<SprykerEco\Yves\Payone\PayoneFactory>.

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...
52
        $customerTransfer = $customerClient->getCustomer();
53
54
        if (!$customerTransfer) {
55
            return $this->redirectResponseInternal(PayoneControllerProvider::ROUTE_LOGIN);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirectRe...Provider::ROUTE_LOGIN); (Symfony\Component\HttpFoundation\RedirectResponse) is incompatible with the return type documented by SprykerEco\Yves\Payone\C...ntroller::getFileAction of type Symfony\Component\HttpFoundation\StreamedResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
56
        }
57
58
        $getFileTransfer = new PayoneGetFileTransfer();
59
        $getFileTransfer->setReference($request->query->get('id'));
60
        $getFileTransfer->setCustomerId($customerTransfer->getIdCustomer());
61
62
        $response = $this->getClient()->getFile($getFileTransfer);
63
64
        if ($response->getStatus() === 'ERROR') {
65
            return $this->viewResponse(['errormessage' => $response->getCustomerErrorMessage()]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->viewRespon...stomerErrorMessage())); (array) is incompatible with the return type documented by SprykerEco\Yves\Payone\C...ntroller::getFileAction of type Symfony\Component\HttpFoundation\StreamedResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
66
        }
67
68
        $callback = function () use ($response) {
69
            echo base64_decode($response->getRawResponse());
70
        };
71
72
        return $this->streamedResponse($callback, 200, ["Content-type" => "application/pdf"]);
73
    }
74
75
    /**
76
     * @param \Symfony\Component\HttpFoundation\Request $request
77
     *
78
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
79
     */
80 View Code Duplication
    public function getInvoiceAction(Request $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...
81
    {
82
        $customerClient = $this->getFactory()->getCustomerClient();
83
        $customerTransfer = $customerClient->getCustomer();
84
85
        if (!$customerTransfer) {
86
            return $this->redirectResponseInternal(PayoneControllerProvider::ROUTE_LOGIN);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirectRe...Provider::ROUTE_LOGIN); (Symfony\Component\HttpFoundation\RedirectResponse) is incompatible with the return type documented by SprykerEco\Yves\Payone\C...oller::getInvoiceAction of type Symfony\Component\HttpFoundation\StreamedResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
87
        }
88
89
        $getInvoiceTransfer = new PayoneGetInvoiceTransfer();
90
        $getInvoiceTransfer->setReference($request->query->get('id'));
91
        $getInvoiceTransfer->setCustomerId($customerTransfer->getIdCustomer());
92
93
        $response = $this->getClient()->getInvoice($getInvoiceTransfer);
94
95
        if ($response->getStatus() === 'ERROR') {
96
            return $this->viewResponse(['errormessage' => $response->getInternalErrorMessage()]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->viewRespon...ternalErrorMessage())); (array) is incompatible with the return type documented by SprykerEco\Yves\Payone\C...oller::getInvoiceAction of type Symfony\Component\HttpFoundation\StreamedResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
97
        }
98
99
        $callback = function () use ($response) {
100
            echo base64_decode($response->getRawResponse());
101
        };
102
103
        return $this->streamedResponse($callback, 200, ["Content-type" => "application/pdf"]);
104
    }
105
106
    /**
107
     * @param \Symfony\Component\HttpFoundation\Request $request
108
     *
109
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
110
     */
111 View Code Duplication
    public function getSecurityInvoiceAction(Request $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...
112
    {
113
        $customerClient = $this->getFactory()->getCustomerClient();
114
        $customerTransfer = $customerClient->getCustomer();
115
116
        if (!$customerTransfer) {
117
            return $this->redirectResponseInternal(PayoneControllerProvider::ROUTE_LOGIN);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirectRe...Provider::ROUTE_LOGIN); (Symfony\Component\HttpFoundation\RedirectResponse) is incompatible with the return type documented by SprykerEco\Yves\Payone\C...etSecurityInvoiceAction of type Symfony\Component\HttpFoundation\StreamedResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
118
        }
119
120
        $getInvoiceTransfer = new PayoneGetInvoiceTransfer();
121
        $getInvoiceTransfer->setReference($request->query->get('id'));
122
        $getInvoiceTransfer->setCustomerId($customerTransfer->getIdCustomer());
123
124
        $response = $this->getClient()->getInvoice($getInvoiceTransfer);
125
126
        if ($response->getStatus() === 'ERROR') {
127
            return $this->viewResponse(['errormessage' => $response->getInternalErrorMessage()]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->viewRespon...ternalErrorMessage())); (array) is incompatible with the return type documented by SprykerEco\Yves\Payone\C...etSecurityInvoiceAction of type Symfony\Component\HttpFoundation\StreamedResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
128
        }
129
130
        $callback = function () use ($response) {
131
            echo base64_decode($response->getRawResponse());
132
        };
133
134
        return $this->streamedResponse($callback, 200, ["Content-type" => "application/pdf"]);
135
    }
136
137
    /**
138
     * @param \Symfony\Component\HttpFoundation\Request $request
139
     *
140
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
141
     */
142
    public function cancelRedirectAction(Request $request)
143
    {
144
        $orderReference = $request->query->get('orderReference');
145
        $urlHmac = $request->query->get('sig');
146
147
        if ($orderReference) {
148
            $cancelRedirectTransfer = new PayoneCancelRedirectTransfer();
149
            $cancelRedirectTransfer->setOrderReference($orderReference);
150
            $cancelRedirectTransfer->setUrlHmac($urlHmac);
151
152
            $response = $this->getClient()->cancelRedirect($cancelRedirectTransfer);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
153
        }
154
155
        return $this->redirectResponseInternal(PayoneControllerProvider::CHECKOUT_PAYMENT);
156
    }
157
158
    /**
159
     * @param callable|null $callback
160
     * @param int $status
161
     * @param array $headers
162
     *
163
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
164
     */
165
    protected function streamedResponse($callback = null, $status = 200, $headers = [])
166
    {
167
        $streamedResponse = new StreamedResponse($callback, $status, $headers);
168
        $streamedResponse->send();
169
170
        return $streamedResponse;
171
    }
172
}
173