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

VendorConnectionRevoker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 32
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A revoke() 0 17 3
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