Completed
Push — master ( 3377ec...b18ded )
by Tharanga
02:10
created

VendorConnectionRevoker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Tharanga Kothalawala <[email protected]>
4
 * @date 17-02-2019
5
 */
6
7
namespace TSK\SSO\ThirdParty;
8
9
use TSK\SSO\Storage\ThirdPartyStorageRepository;
10
11
/**
12
 * @package TSK\SSO\ThirdParty
13
 *
14
 * This class is capable of revoking vendor connection access
15
 */
16
class VendorConnectionRevoker
17
{
18
    /**
19
     * @var VendorConnection
20
     */
21
    private $thirdPartyConnection;
22
23
    /**
24
     * @var ThirdPartyStorageRepository
25
     */
26
    private $storageRepository;
27
28
    /**
29
     * @param VendorConnection $thirdPartyConnection vendor connection to use for the revoke action
30
     * @param ThirdPartyStorageRepository $storageRepository the storage implementation
31
     *        which was used to store the data for the first time.
32
     */
33 3
    public function __construct(
34
        VendorConnection $thirdPartyConnection,
35
        ThirdPartyStorageRepository $storageRepository
36
    ) {
37 3
        $this->thirdPartyConnection = $thirdPartyConnection;
38 3
        $this->storageRepository = $storageRepository;
39 3
    }
40
41
    /**
42
     * Use this to revoke the app's access to the third party and to remove the local vendor user mappings.
43
     *
44
     * @return bool
45
     */
46 3
    public function revoke($vendorEmail, $vendorName)
47
    {
48
        // return false if no vendor user mapping found
49 3
        $mappedUser = $this->storageRepository->getUser($vendorEmail, $vendorName);
50 3
        if (is_null($mappedUser)) {
51 1
            return false;
52
        }
53
54 2
        $isRevoked = $this->thirdPartyConnection->revokeAccess(
55 2
            new CommonAccessToken($mappedUser->vendorToken(), $mappedUser->vendorName(), $mappedUser->vendorEmail())
56 2
        );
57
58 2
        if (!$isRevoked) {
59 1
            return false;
60
        }
61
62 1
        return $this->storageRepository->remove($vendorEmail, $vendorName);
63
    }
64
}
65