AccountController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A redirectAction() 0 18 4
A referralAction() 0 22 4
1
<?php
2
3
4
namespace PTS\SyliusReferralPlugin\Controller;
5
6
use PTS\SyliusReferralPlugin\Entity\Customer;
7
use PTS\SyliusReferralPlugin\Service\CustomerManager;
8
use Sylius\Component\Core\Model\ShopUser;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Session\Session;
12
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
13
14
class AccountController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead. ( Ignorable by Annotation )

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

14
class AccountController extends /** @scrutinizer ignore-deprecated */ Controller
Loading history...
15
{
16
    public function referralAction(Request $request)
17
    {
18
        $token = $this->get('security.token_storage')->getToken();
19
        $customerManager = $this->get('app.customer.manager');
20
        if ($token) {
21
            /** @var ShopUser $user */
22
            $user = $token->getUser();
23
            if ($user && $user instanceof ShopUser) {
0 ignored issues
show
introduced by
$user is always a sub-type of Sylius\Component\Core\Model\ShopUser.
Loading history...
24
                $page = $request->get('page');
25
                $enroller = $user->getCustomer();
26
                $routeName = $request->get('_route');
27
                $data = $customerManager->getPaginatedCustomers($enroller, $page, $routeName);
28
                return $this->render(
29
                    'Account\customers.html.twig',
30
                    [
31
                        'customers' => $data['customers'],
32
                        'pagination' => $data['pagination']
33
                    ]
34
                );
35
            }
36
        }
37
        throw new BadRequestHttpException('Unauthorized to view this resource');
38
    }
39
40
    public function redirectAction(Request $request)
41
    {
42
        $token = $request->get('redirectToken');
43
        $url = $request->get('redirectUrl');
44
        if ($token) {
45
            $user = $this->get('sylius.repository.shop_user')->findOneBy(['redirectToken' => $token]);
46
            if ($user) {
47
                $user->setRedirectToken('');
48
                $this->get('doctrine.orm.default_entity_manager')->flush($user);
49
                $this->get('sylius.security.user_login')->login($user, 'shop');
50
                $currentChannel = $this->get('sylius.context.channel')->getChannel();
51
                $this->get('app.manager.cart')->checkCart($user->getCustomer(), $currentChannel, $request->getSession());
52
            }
53
        }
54
        if ($url) {
55
            return $this->redirect($url);
56
        } else {
57
            return $this->redirectToRoute('sylius_shop_homepage');
58
        }
59
    }
60
}
61