Completed
Pull Request — master (#4)
by Tharanga
04:39
created

VendorConnectionRevoker::revoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 12
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
    public function __construct(
19
        VendorConnection $thirdPartyConnection,
20
        ThirdPartyStorageRepository $storageRepository
21
    ) {
22
        $this->thirdPartyConnection = $thirdPartyConnection;
0 ignored issues
show
Bug Best Practice introduced by
The property thirdPartyConnection does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
        $this->storageRepository = $storageRepository;
0 ignored issues
show
Bug Best Practice introduced by
The property storageRepository does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
    }
25
26
    /**
27
     * Use this to revoke the app's access to the third party and to remove the local vendor user mappings.
28
     *
29
     * @return bool
30
     */
31
    public function revoke($vendorEmail, $vendorName)
32
    {
33
        // return false if no vendor user mapping found
34
        $mappedUser = $this->storageRepository->getUser($vendorEmail, $vendorName);
35
        if (is_null($mappedUser)) {
36
            return false;
37
        }
38
39
        $isRevoked = $this->thirdPartyConnection->revokeAccess(
40
            new CommonAccessToken($mappedUser->vendorToken(), $mappedUser->vendorName(), $mappedUser->vendorEmail())
41
        );
42
43
        if (!$isRevoked) {
44
            return false;
45
        }
46
47
        return $this->storageRepository->remove($vendorEmail, $vendorName);
48
    }
49
}
50