Issues (3877)

Oauth/Business/Mapper/OauthRefreshTokenMapper.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\Oauth\Business\Mapper;
9
10
use Generated\Shared\Transfer\OauthRefreshTokenTransfer;
11
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
12
use Spryker\Zed\Oauth\Dependency\Service\OauthToUtilEncodingServiceInterface;
13
14
class OauthRefreshTokenMapper implements OauthRefreshTokenMapperInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected const CUSTOMER_REFERENCE = 'customer_reference';
20
21
    /**
22
     * @var \Spryker\Zed\Oauth\Dependency\Service\OauthToUtilEncodingServiceInterface
23
     */
24
    protected $utilEncodingService;
25
26
    /**
27
     * @var array<\Spryker\Zed\OauthExtension\Dependency\Plugin\OauthUserIdentifierFilterPluginInterface>
28
     */
29
    protected $oauthUserIdentifierFilterPlugins;
30
31
    /**
32
     * @param \Spryker\Zed\Oauth\Dependency\Service\OauthToUtilEncodingServiceInterface $utilEncodingService
33
     * @param array<\Spryker\Zed\OauthExtension\Dependency\Plugin\OauthUserIdentifierFilterPluginInterface> $oauthUserIdentifierFilterPlugins
34
     */
35
    public function __construct(
36
        OauthToUtilEncodingServiceInterface $utilEncodingService,
37
        array $oauthUserIdentifierFilterPlugins = []
38
    ) {
39
        $this->utilEncodingService = $utilEncodingService;
40
        $this->oauthUserIdentifierFilterPlugins = $oauthUserIdentifierFilterPlugins;
41
    }
42
43
    /**
44
     * @param \League\OAuth2\Server\Entities\RefreshTokenEntityInterface $refreshTokenEntity
45
     * @param \Generated\Shared\Transfer\OauthRefreshTokenTransfer $oauthRefreshTokenTransfer
46
     *
47
     * @return \Generated\Shared\Transfer\OauthRefreshTokenTransfer
48
     */
49
    public function mapRefreshTokenEntityToOauthRefreshTokenTransfer(
50
        RefreshTokenEntityInterface $refreshTokenEntity,
51
        OauthRefreshTokenTransfer $oauthRefreshTokenTransfer
52
    ): OauthRefreshTokenTransfer {
53
        /** @var string $encodedUserIdentifier */
54
        $encodedUserIdentifier = $refreshTokenEntity->getAccessToken()->getUserIdentifier();
55
        $userIdentifier = $this->utilEncodingService->decodeJson($encodedUserIdentifier, true);
56
        $filteredUserIdentifier = $this->filterUserIdentifier($userIdentifier);
0 ignored issues
show
It seems like $userIdentifier can also be of type null; however, parameter $userIdentifier of Spryker\Zed\Oauth\Busine...:filterUserIdentifier() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

56
        $filteredUserIdentifier = $this->filterUserIdentifier(/** @scrutinizer ignore-type */ $userIdentifier);
Loading history...
57
        $encodedUserIdentifier = (string)$this->utilEncodingService->encodeJson($filteredUserIdentifier);
58
59
        $oauthRefreshTokenTransfer->setIdentifier($refreshTokenEntity->getIdentifier())
60
            ->setCustomerReference($userIdentifier[static::CUSTOMER_REFERENCE] ?? null)
61
            ->setUserIdentifier($encodedUserIdentifier)
62
            ->setExpiresAt($refreshTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s'))
63
            ->setIdOauthClient($refreshTokenEntity->getAccessToken()->getClient()->getIdentifier())
64
            ->setScopes($this->utilEncodingService->encodeJson($refreshTokenEntity->getAccessToken()->getScopes()));
65
66
        return $oauthRefreshTokenTransfer;
67
    }
68
69
    /**
70
     * @param array $userIdentifier
71
     *
72
     * @return array
73
     */
74
    protected function filterUserIdentifier(array $userIdentifier): array
75
    {
76
        foreach ($this->oauthUserIdentifierFilterPlugins as $oauthUserIdentifierFilterPlugin) {
77
            $userIdentifier = $oauthUserIdentifierFilterPlugin->filter($userIdentifier);
78
        }
79
80
        return $userIdentifier;
81
    }
82
}
83