Passed
Pull Request — master (#9)
by Daniel
02:12
created

JwtController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 63
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A showLanding() 0 18 3
A __construct() 0 7 1
1
<?php
2
3
namespace CodeCloud\Bundle\ShopifyBundle\Controller;
4
5
use CodeCloud\Bundle\ShopifyBundle\Model\ShopifyStoreManagerInterface;
6
use CodeCloud\Bundle\ShopifyBundle\Service\JwtResolver;
7
use Firebase\JWT\JWT;
0 ignored issues
show
Bug introduced by
The type Firebase\JWT\JWT 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...
8
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
9
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
10
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFo...ion\BadRequestException 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 Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15
use Twig\Environment;
0 ignored issues
show
Bug introduced by
The type Twig\Environment 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...
16
use Symfony\Component\Routing\Annotation\Route;
17
18
/**
19
 * Handles JWT resolution to obtain a session id.
20
 *
21
 * @see https://shopify.dev/tutorials/authenticate-your-app-using-session-tokens#session-tokens
22
 *
23
 * @Route("/shopify")
24
 */
25
class JwtController
26
{
27
    /**
28
     * @var UrlGeneratorInterface
29
     */
30
    private $router;
31
32
    /**
33
     * @var Environment
34
     */
35
    private $templating;
36
37
    /**
38
     * @var JwtResolver
39
     */
40
    private $jwtResolver;
41
42
    /**
43
     * @var ShopifyStoreManagerInterface
44
     */
45
    private $storeManager;
46
47
    /**
48
     * @var string
49
     */
50
    private $redirectRoute;
51
52
    /**
53
     * @param UrlGeneratorInterface $router
54
     * @param Environment $templating
55
     * @param JwtResolver $jwtResolver
56
     * @param string $redirectRoute
57
     */
58
    public function __construct(UrlGeneratorInterface $router, Environment $templating, JwtResolver $jwtResolver, ShopifyStoreManagerInterface  $storeManager, string $redirectRoute)
59
    {
60
        $this->router = $router;
61
        $this->templating = $templating;
62
        $this->jwtResolver = $jwtResolver;
63
        $this->storeManager = $storeManager;
64
        $this->redirectRoute = $redirectRoute;
65
    }
66
67
    /**
68
     * @Route("/jwt", name="codecloud_shopify_jwt")
69
     */
70
    public function showLanding(Request $request)
71
    {
72
        if ($request->isMethod('GET')) {
73
            return new Response($this->templating->render('@CodeCloudShopify/jwt.html.twig', [
74
                'shop' => $request->query->get('shop'),
75
            ]));
76
        }
77
78
        if (!$jwt = $request->request->get('jwt')) {
79
            throw new BadRequestException('missing jwt token');
80
        }
81
82
        $session = $this->jwtResolver->resolveJwt($jwt);
83
        $this->storeManager->authenticateSession($session);
84
85
        return new RedirectResponse(
86
            $this->router->generate($this->redirectRoute, [
87
                'shopify_session_id' => $session->sessionId,
88
            ])
89
        );
90
    }
91
}
92