JWEDecrypterAbstractFactory::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 19
ccs 8
cts 10
cp 0.8
rs 9.8666
cc 2
nc 2
nop 3
crap 2.032
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\JWTModule\DIFactory\Encryption;
6
7
use Interop\Container\ContainerInterface;
8
use Interop\Container\Exception\ContainerException;
9
use Jose\Component\Encryption\JWEDecrypter;
10
use Jose\Component\Encryption\JWEDecrypterFactory;
11
use TMV\JWTModule\DIFactory\AbstractServiceFactory;
12
use TMV\JWTModule\Exception\InvalidArgumentException;
13
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
14
use Zend\ServiceManager\Exception\ServiceNotFoundException;
15
16
class JWEDecrypterAbstractFactory extends AbstractServiceFactory
17
{
18
    private const SERVICE_TYPE_KEY = 'jwe_decrypter';
19
20
    /**
21
     * Create an object
22
     *
23
     * @param ContainerInterface $container
24
     * @param string $requestedName
25
     * @param null|array $options
26
     *
27
     * @throws ServiceNotFoundException if unable to resolve the service
28
     * @throws ServiceNotCreatedException if an exception is raised when
29
     *     creating a service
30
     * @throws ContainerException if any other error occurs
31
     *
32
     * @return JWEDecrypter
33
     */
34 2
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): JWEDecrypter
35
    {
36
        try {
37 2
            $keyConfig = $this->getServiceConfig($container, $requestedName);
38
        } catch (InvalidArgumentException $e) {
39
            throw new ServiceNotCreatedException('Unable to find service for ' . $requestedName, 0, $e);
40
        }
41
42 2
        $keyEncryptionAlgorithms = $keyConfig['key_encryption_algorithms'] ?? [];
43 2
        $contentEncryptionAlgorithms = $keyConfig['content_encryption_algorithms'] ?? [];
44 2
        $compressionMethods = $keyConfig['compression_methods'] ?? [];
45
46
        /** @var JWEDecrypterFactory $factory */
47 2
        $factory = $container->get(JWEDecrypterFactory::class);
48
49 2
        return $factory->create(
50 2
            $keyEncryptionAlgorithms,
51
            $contentEncryptionAlgorithms,
52
            $compressionMethods
53
        );
54
    }
55
56 6
    protected function getServiceTypeName(): string
57
    {
58 6
        return static::SERVICE_TYPE_KEY;
59
    }
60
}
61