GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 96d802...e5ec4d )
by Joni
04:39
created

AttributeCertificate   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 156
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 14
c 5
b 0
f 1
lcom 2
cbo 8
dl 156
loc 156
ccs 39
cts 39
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A fromDER() 3 3 1
A fromPEM() 6 6 2
A acinfo() 3 3 1
A signatureAlgorithm() 3 3 1
A signatureValue() 3 3 1
A toASN1() 5 5 1
A toDER() 3 3 1
A toPEM() 3 3 1
A verify() 5 5 1
A __toString() 3 3 1
A fromASN1() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace X509\AttributeCertificate;
4
5
use ASN1\Type\Constructed\Sequence;
6
use CryptoUtil\ASN1\AlgorithmIdentifier;
7
use CryptoUtil\ASN1\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier;
8
use CryptoUtil\ASN1\PublicKeyInfo;
9
use CryptoUtil\Crypto\Crypto;
10
use CryptoUtil\Crypto\Signature;
11
use CryptoUtil\PEM\PEM;
12
13
14
/**
15
 * Implements <i>AttributeCertificate</i> ASN.1 type.
16
 *
17
 * @link https://tools.ietf.org/html/rfc5755#section-4.1
18
 */
19 View Code Duplication
class AttributeCertificate
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
	/**
22
	 * Attribute certificate info.
23
	 *
24
	 * @var AttributeCertificateInfo $_acinfo
25
	 */
26
	protected $_acinfo;
27
	
28
	/**
29
	 * Signature algorithm identifier.
30
	 *
31
	 * @var SignatureAlgorithmIdentifier $_signatureAlgorithm
32
	 */
33
	protected $_signatureAlgorithm;
34
	
35
	/**
36
	 * Signature value.
37
	 *
38
	 * @var Signature $_signatureValue
39
	 */
40
	protected $_signatureValue;
41
	
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param AttributeCertificateInfo $acinfo
46
	 * @param SignatureAlgorithmIdentifier $algo
47
	 * @param Signature $signature
48
	 */
49 5
	public function __construct(AttributeCertificateInfo $acinfo, 
50
			SignatureAlgorithmIdentifier $algo, Signature $signature) {
51 5
		$this->_acinfo = $acinfo;
52 5
		$this->_signatureAlgorithm = $algo;
53 5
		$this->_signatureValue = $signature;
54 5
	}
55
	
56
	/**
57
	 * Initialize from ASN.1.
58
	 *
59
	 * @param Sequence $seq
60
	 * @return self
61
	 */
62 4
	public static function fromASN1(Sequence $seq) {
63 4
		$acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence());
64 4
		$algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence());
65 4
		if (!$algo instanceof SignatureAlgorithmIdentifier) {
66 1
			throw new \UnexpectedValueException(
67 1
				"Unsupported signature algorithm " . $algo->oid() . ".");
68
		}
69 3
		$signature = Signature::fromASN1($seq->at(2)->asBitString());
70 3
		return new self($acinfo, $algo, $signature);
71
	}
72
	
73
	/**
74
	 * Initialize from DER data.
75
	 *
76
	 * @param string $data
77
	 * @return self
78
	 */
79 1
	public static function fromDER($data) {
80 1
		return self::fromASN1(Sequence::fromDER($data));
81
	}
82
	
83
	/**
84
	 * Initialize from PEM.
85
	 *
86
	 * @param PEM $pem
87
	 * @throws \UnexpectedValueException
88
	 * @return self
89
	 */
90 2
	public static function fromPEM(PEM $pem) {
91 2
		if ($pem->type() !== PEM::TYPE_ATTRIBUTE_CERTIFICATE) {
92 1
			throw new \UnexpectedValueException("Invalid PEM type.");
93
		}
94 1
		return self::fromDER($pem->data());
95
	}
96
	
97
	/**
98
	 * Get attribute certificate info.
99
	 *
100
	 * @return AttributeCertificateInfo
101
	 */
102 2
	public function acinfo() {
103 2
		return $this->_acinfo;
104
	}
105
	
106
	/**
107
	 * Get signature algorithm identifier.
108
	 *
109
	 * @return SignatureAlgorithmIdentifier
110
	 */
111 2
	public function signatureAlgorithm() {
112 2
		return $this->_signatureAlgorithm;
113
	}
114
	
115
	/**
116
	 * Get signature value.
117
	 *
118
	 * @return Signature
119
	 */
120 1
	public function signatureValue() {
121 1
		return $this->_signatureValue;
122
	}
123
	
124
	/**
125
	 * Get ASN.1 structure.
126
	 *
127
	 * @return Sequence
128
	 */
129 4
	public function toASN1() {
130 4
		return new Sequence($this->_acinfo->toASN1(), 
131 4
			$this->_signatureAlgorithm->toASN1(), 
132 4
			$this->_signatureValue->toBitString());
133
	}
134
	
135
	/**
136
	 * Get attribute certificate as a DER.
137
	 *
138
	 * @return string
139
	 */
140 2
	public function toDER() {
141 2
		return $this->toASN1()->toDER();
142
	}
143
	
144
	/**
145
	 * Get attribute certificate as a PEM.
146
	 *
147
	 * @return PEM
148
	 */
149 2
	public function toPEM() {
150 2
		return new PEM(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER());
151
	}
152
	
153
	/**
154
	 * Verify signature.
155
	 *
156
	 * @param Crypto $crypto
157
	 * @param PublicKeyInfo $pubkey_info Signer's public key
158
	 * @return bool
159
	 */
160 2
	public function verify(Crypto $crypto, PublicKeyInfo $pubkey_info) {
161 2
		$data = $this->_acinfo->toASN1()->toDER();
162 2
		return $crypto->verify($data, $this->_signatureValue, $pubkey_info, 
163 2
			$this->_signatureAlgorithm);
164
	}
165
	
166
	/**
167
	 * Get attribute certificate as a PEM formatted string.
168
	 *
169
	 * @return string
170
	 */
171 1
	public function __toString() {
172 1
		return $this->toPEM()->string();
173
	}
174
}
175