Completed
Pull Request — master (#15)
by Cesar
01:41
created

SessionService::updateCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Service\Paypal;
4
5
use PayPal\Api\OpenIdUserinfo;
6
use Psr\Log\LoggerInterface;
7
use Symfony\Component\HttpFoundation\Session\Session;
8
use Symfony\Component\HttpFoundation\Session\SessionInterface;
9
10
/**
11
 * Class SessionService
12
 * @package App\Service\PayPal
13
 */
14
class SessionService
15
{
16
    /**
17
     * @var Session
18
     */
19
    public $session;
20
21
    /**
22
     * @var LoggerInterface
23
     */
24
    private $logger;
25
26
    /**
27
     * SessionService constructor.
28
     * @param SessionInterface $session
29
     * @param LoggerInterface $logger
30
     */
31
    public function __construct(SessionInterface  $session, LoggerInterface $logger)
32
    {
33
        $this->session = $session;
0 ignored issues
show
Documentation Bug introduced by
$session is of type object<Symfony\Component...ssion\SessionInterface>, but the property $session was declared to be of type object<Symfony\Component...dation\Session\Session>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
34
        $this->logger = $logger;
35
    }
36
37
    /**
38
     * @return bool
39
     */
40
    public function isActive(): bool
41
    {
42
        if (is_array($this->session->get('paypal'))) {
43
            return true;
44
        }
45
        return false;
46
    }
47
48
    /**
49
     * @param OpenIdUserinfo $userinfo
50
     * @param string $refreshToken
51
     */
52
    public function login(OpenIdUserinfo $userinfo, string $refreshToken): void
53
    {
54
        $paypalUser = [
55
            'email' => $userinfo->getEmail(),
56
            'name' => $userinfo->getName(),
57
            'userId' => $userinfo->getUserId(),
58
            'address_street' => $userinfo->getAddress()->getStreetAddress(),
59
            'address_locality' => $userinfo->getAddress()->getLocality(),
60
            'address_region' => $userinfo->getAddress()->getRegion(),
61
            'address_post_code' => $userinfo->getAddress()->getPostalCode(),
62
            'address_country' => $userinfo->getAddress()->getCountry(),
63
            'refresh-token' => $refreshToken,
64
        ];
65
        $this->session->set('paypal', $paypalUser);
66
    }
67
68
    /**
69
     * LogOut from Connect with PayPal
70
     */
71
    public function logout(): void
72
    {
73
        $this->session->remove('paypal');
74
    }
75
76
    /**
77
     * @return string|null
78
     */
79
    public function getRefreshToken(): ?string
80
    {
81
        return $this->session->get('paypal')['refresh-token'];
82
    }
83
84
    /**
85
     * @param string $clientId
86
     * @param string $clientSecret
87
     */
88
    public function updateCredentials(string $clientId, string $clientSecret): void
89
    {
90
        $this->session->set('PAYPAL_SDK_CLIENT_ID', $clientId);
91
        $this->session->set('PAYPAL_SDK_CLIENT_SECRET', $clientSecret);
92
    }
93
94
    /**
95
     * Remove PayPal SandBox credentials
96
     */
97
    public function removeCredentials(): void
98
    {
99
        $this->session->remove('PAYPAL_SDK_CLIENT_ID');
100
        $this->session->remove('PAYPAL_SDK_CLIENT_SECRET');
101
    }
102
}
103