Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
CRAP
75.00% covered (warning)
75.00%
9 / 12
Parser
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
6.56
75.00% covered (warning)
75.00%
9 / 12
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 parseUnverified
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 parseUsingOnlyTrustedCerts
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 parsePayload
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 convertTimestampMs
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
1<?php
2
3namespace Proton\IosReceiptParser;
4
5use Proton\IosReceiptParser\ASN1\Pkcs7Reader;
6use Proton\IosReceiptParser\ASN1\Pkcs7UnverifiedParser;
7use Proton\IosReceiptParser\ASN1\SimpleDecoder;
8use DateTime;
9
10final class Parser
11{
12    /** @var Pkcs7Reader */
13    private $pkcs7Reader;
14
15    /** @var SimpleDecoder */
16    private $decoder;
17
18    public function __construct(Pkcs7Reader $pkcs7Reader = null)
19    {
20        $this->pkcs7Reader = $pkcs7Reader ?? new Pkcs7UnverifiedParser();
21        $this->decoder = new SimpleDecoder();
22    }
23
24    public function parseUnverified(string $receipt): Receipt
25    {
26        $payload = $this->pkcs7Reader->readUnverified(base64_decode($receipt));
27
28        return $this->parsePayload($payload);
29    }
30
31    /**
32     * @throws \Exception
33     */
34    public function parseUsingOnlyTrustedCerts(string $receipt, string ...$certificate): Receipt
35    {
36        $payload = $this->pkcs7Reader->readUsingOnlyTrustedCerts(base64_decode($receipt), ...$certificate);
37
38        return $this->parsePayload($payload);
39    }
40
41    private function parsePayload(string $payload): Receipt
42    {
43        return new Receipt($this->decoder->decodeAttributesSet($payload), $this->decoder);
44    }
45
46    public static function convertTimestampMs(?string $time): ?string
47    {
48        if ($time === null) {
49            return null;
50        }
51        $datetime = DateTime::createFromFormat(DateTime::ATOM, $time);
52        return (string) $datetime->getTimestamp() . $datetime->format('v');
53    }
54}