Completed
Pull Request — master (#3)
by methylbro
03:12
created

Link::generate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 57
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 9.0309
c 0
b 0
f 0
cc 4
eloc 20
nc 5
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the Mediapart LaPresseLibre Library.
5
 *
6
 * CC BY-NC-SA <https://github.com/mediapart/lapresselibre>
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mediapart\LaPresseLibre\Account;
13
14
use Mediapart\LaPresseLibre\Security\Encryption;
15
use Mediapart\LaPresseLibre\Account\Account;
16
use Mediapart\LaPresseLibre\Account\Repository;
17
18
/**
19
 * @see https://github.com/NextINpact/LaPresseLibreSDK/wiki/Liaison-de-compte-utilisateur-par-redirection
20
 */
21
class Link
22
{
23
    const RETURN_URL = 'https://beta.lapresselibre.fr/manage/link-result?lpl=%1$s&part=%2$s';
24
    const STATUS_SUCCESS = 1;
25
    const STATUS_FAILURE = 2;
26
    const STATUS_CONFLICT = 3;
27
28
    /**
29
     * @var Encryption
30
     */
31
    private $encryption;
32
33
    /**
34
     * @var Repository 
35
     */
36
    private $repository;
37
38
    /**
39
     * @var int
40
     */
41
    private $public_key;
42
43
    /**
44
     * @param Encryption $encryption
45
     * @param Repository $repository
46
     * @param int $public_key
47
     */
48
    public function __construct(Encryption $encryption, Repository $repository, $public_key)
49
    {
50
        $this->encryption = $encryption;
51
        $this->repository = $repository;
52
        $this->public_key = $public_key;
53
    }
54
55
    /**
56
     * Liaison de compte utilisateur par redirection
57
     *
58
     * @param string $lplUser
59
     * @param Account $logguedAccount
60
     * @return string
61
     */
62
    public function generate($lplUser, Account $logguedAccount)
63
    {
64
        /* Le paramètre "lpluser" représente l'ID LPL de l'utilisateur qui 
65
           souhaite lier son compte. Il est chiffré en AES256 puis codé en 
66
           base64 en reprenant la méthode de chiffrement utilisée pour les 
67
           web services. */
68
        $code = $this->encryption->decrypt($lplUser);
69
70
        if ($existingAccount = $this->repository->find($code)) {
71
72
            /* En cas de conflit la valeur du statut que le partenaire doit
73
               retourner sera "3". Sauf évidement s'il s'agit du bon compte
74
               utilisateur. */
75
            $status = ($existingAccount != $logguedAccount) 
76
                ? self::STATUS_CONFLICT 
77
                : self::STATUS_SUCCESS
78
            ;
79
80
        } else {
81
            try {
82
83
                /* Si l'ID LPL reçu n'est pas déjà présent, le partenaire 
84
                   doit rechercher le compte utilisateur pour y rattacher 
85
                   L'ID LPL. Puis on retourne un statut "1" pour indiquer 
86
                   que la liaison s'est effectuée avec succès. */
87
                $account = new Account($logguedAccount->getEmail(), $code);
88
                $this->repository->save($account);
89
                $status = self::STATUS_SUCCESS;
90
91
            } catch (\Exception $e) {
92
93
                /* Le statut retourné par le partenaire LPL est "2" en cas 
94
                   d'erreur. */
95
                $status = self::STATUS_FAILURE;
96
            }
97
        }
98
99
        /* Le partenaire doit rediriger l'utilisateur vers l'url fournie par 
100
           LPL avec les paramètres : */
101
        return sprintf(
102
            self::RETURN_URL,
103
104
            /* "lpl" : composé de l'ID LPL et du statut. Ce paramètre sera 
105
               ensuite chiffré en AES puis codé en base64. 
106
               Exemple : { Guid: xxxx, statut: 1 } */
107
            rawurlencode(
108
                $this->encryption->encrypt(
109
                    [
0 ignored issues
show
Bug introduced by
array('Guid' => $code, 'statut' => $status) of type array<string,integer|string> is incompatible with the type string expected by parameter $message of Mediapart\LaPresseLibre\...y\Encryption::encrypt(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
                    /** @scrutinizer ignore-type */ [
Loading history...
110
                        'Guid' => $code,
111
                        'statut' => $status,
112
                    ],
113
                    OPENSSL_RAW_DATA & OPENSSL_NO_PADDING
114
                )
115
            ),
116
117
            /* "part" : qui représente le code du partenaire. */
118
            $this->public_key
119
        );
120
    }
121
}
122