EncryptionBaseClass   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 2
dl 0
loc 59
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
__invoke() 0 1 ?
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Copyright (c) 2017-present Ganbaro Digital Ltd
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 *
13
 *   * Redistributions of source code must retain the above copyright
14
 *     notice, this list of conditions and the following disclaimer.
15
 *
16
 *   * Redistributions in binary form must reproduce the above copyright
17
 *     notice, this list of conditions and the following disclaimer in
18
 *     the documentation and/or other materials provided with the
19
 *     distribution.
20
 *
21
 *   * Neither the names of the copyright holders nor the names of his
22
 *     contributors may be used to endorse or promote products derived
23
 *     from this software without specific prior written permission.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
 * POSSIBILITY OF SUCH DAMAGE.
37
 *
38
 * @category  Libraries
39
 * @package   MessagingPipeline/Instructions
40
 * @author    Stuart Herbert <[email protected]>
41
 * @copyright 2017-present Ganbaro Digital Ltd www.ganbarodigital.com
42
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
43
 * @link      http://ganbarodigital.github.io/php-mv-messaging-pipeline
44
 */
45
46
namespace GanbaroDigital\MessagingPipeline\V1\Instructions;
47
48
use GanbaroDigital\MessagingPipeline\V1\InstructionTypes;
49
use GanbaroDigital\MessagingPipeline\V1\NextInstruction;
50
use GanbaroDigital\MessagingPipeline\V1\Operations;
51
use GanbaroDigital\MessagingPipeline\V1\Requirements;
52
53
/**
54
 * base class for all MessagingPipelineInstructions that work with
55
 * encryption
56
 */
57
abstract class EncryptionBaseClass implements InstructionTypes\StringInMixedOut
58
{
59
    /**
60
     * what encryption cipher are we using?
61
     *
62
     * @var string
63
     */
64
    protected $encryptionType;
65
66
    /**
67
     * a shared secret password, used for encrypting and decrypting the data
68
     *
69
     * @var string
70
     */
71
    protected $encryptionKey;
72
73
    /**
74
     * the initialisation vector, which we are calling a 'shared secret'
75
     *
76
     * @var string
77
     */
78
    protected $encryptionSecret;
79
80
    /**
81
     * creates a MessagingPipelineInstruction that's ready to use
82
     *
83
     * expects the following items in $config
84
     * - $config['type'] - the encryption cipher
85
     * - $config['key'] - a shared password
86
     * - $config['secret'] - the initialisation vector
87
     *
88
     * @param array $config
89
     *        the config we need to do our work
90
     */
91
    public function __construct(array $config)
92
    {
93
        // robustness!!
94
        Requirements\RequireConfigHasKey::apply('type')->to($config);
95
        Requirements\RequireConfigHasKey::apply('key')->to($config);
96
        Requirements\RequireConfigHasKey::apply('secret')->to($config);
97
98
        $this->encryptionType = $config['type'];
99
        $this->encryptionKey = $config['key'];
100
        $this->encryptionSecret = $config['secret'];
101
    }
102
103
    /**
104
     * executes whatever work this messaging pipeline instruction
105
     * is here to do
106
     *
107
     * @param  NextInstruction $next
108
     *         the instruction to call when our work is done
109
     * @param  string $payload
110
     *         the data that we're processing
111
     * @return mixed
112
     *         whatever $next() returns when we call it
113
     */
114
    abstract public function __invoke(NextInstruction $next, string $payload);
115
}