Completed
Push — master ( e0db1d...82088e )
by Paweł
11:28
created

AbstractSecuredTopic::secure()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 6
nop 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2018 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\WebSocket\Topic;
18
19
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
20
use Gos\Bundle\WebSocketBundle\Server\Exception\FirewallRejectionException;
21
use Gos\Bundle\WebSocketBundle\Topic\SecuredTopicInterface;
22
use Psr\Log\LoggerInterface;
23
use Ratchet\ConnectionInterface;
24
use Ratchet\Wamp\Topic;
25
use SWP\Bundle\CoreBundle\Repository\ApiKeyRepositoryInterface;
26
27
abstract class AbstractSecuredTopic implements SecuredTopicInterface
28
{
29
    /**
30
     * @var ApiKeyRepositoryInterface
31
     */
32
    protected $apiKeyRepository;
33
34
    /**
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
39
    /**
40
     * AbstractSecuredTopic constructor.
41
     *
42
     * @param ApiKeyRepositoryInterface $apiKeyRepository
43
     */
44
    public function __construct(ApiKeyRepositoryInterface $apiKeyRepository, LoggerInterface $logger)
45
    {
46
        $this->apiKeyRepository = $apiKeyRepository;
47
        $this->logger = $logger;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function secure(ConnectionInterface $conn = null, Topic $topic, WampRequest $request, $payload = null, $exclude = null, $eligible = null, $provider = null)
54
    {
55
        $httpRequest = $conn->httpRequest;
0 ignored issues
show
Bug introduced by
Accessing httpRequest on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
56
        $token = null;
57
        parse_str($httpRequest->getUri()->getQuery(), $params);
58
59
        if (isset($params['token'])) {
60
            $token = (string) $params['token'];
61
        }
62
63
        if (null !== $token) {
64
            $this->logger->info(
65
                'Token was found in the request',
66
                ['remoteAddress' => $conn->remoteAddress, 'connectionId' => $conn->resourceId]
0 ignored issues
show
Bug introduced by
Accessing remoteAddress on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing resourceId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
67
            );
68
69
            $apiKey = $this->apiKeyRepository
70
                ->getValidToken($token)
71
                ->getQuery()
72
                ->getOneOrNullResult();
73
74
            if (null === $apiKey) {
75
                throw new FirewallRejectionException();
76
            }
77
        } else {
78
            $this->logger->warning(
79
                'Token was not found in the request',
80
                ['remoteAddress' => $conn->remoteAddress, 'connectionId' => $conn->resourceId]
0 ignored issues
show
Bug introduced by
Accessing remoteAddress on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing resourceId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
81
            );
82
        }
83
    }
84
}
85