Passed
Push — 6.5.0.0 ( b6c247...287512 )
by Christian
24:29 queued 11:17
created

ReferenceInvoiceLoader::load()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 30
nc 6
nop 3
dl 0
loc 50
rs 8.8177
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Document\Service;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Checkout\Document\Renderer\InvoiceRenderer;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Log\Package;
9
use Shopware\Core\Framework\Uuid\Uuid;
10
11
/**
12
 * @internal - Fetch the $referenceDocumentId if set, otherwise fetch the latest document
13
 */
14
#[Package('customer-order')]
15
final class ReferenceInvoiceLoader
16
{
17
    /**
18
     * @internal
19
     */
20
    public function __construct(private readonly Connection $connection)
21
    {
22
    }
23
24
    /**
25
     * @return array<string, string>
26
     */
27
    public function load(string $orderId, ?string $referenceDocumentId = null, ?string $deepLinkCodeRendererConfig = null): array
28
    {
29
        $builder = $this->connection->createQueryBuilder();
30
31
        $builder->select([
32
            'LOWER(HEX(`document`.`id`)) as id',
33
            'LOWER(HEX(`document`.`order_id`)) as orderId',
34
            'LOWER(HEX(`document`.`order_version_id`)) as orderVersionId',
35
            'LOWER(HEX(`order`.`version_id`)) as versionId',
36
            '`order`.`deep_link_code` as deepLinkCode',
37
            '`document`.`config` as config',
38
        ])->from('`document`', '`document`')
39
            ->innerJoin('`document`', '`document_type`', '`document_type`', '`document`.`document_type_id` = `document_type`.`id`')
40
            ->innerJoin('`document`', '`order`', '`order`', '`document`.`order_id` = `order`.`id`');
41
42
        $builder->where('`document_type`.`technical_name` = :techName')
43
            ->andWhere('`document`.`order_id` = :orderId');
44
45
        $builder->setParameters([
46
            'techName' => InvoiceRenderer::TYPE,
47
            'orderId' => Uuid::fromHexToBytes($orderId),
48
        ]);
49
50
        $builder->orderBy('`document`.`updated_at`', 'DESC');
51
52
        if (!empty($referenceDocumentId)) {
53
            $builder->andWhere('`document`.`id` = :documentId');
54
            $builder->setParameter('documentId', Uuid::fromHexToBytes($referenceDocumentId));
55
        }
56
57
        $documents = $builder->executeQuery()->fetchAllAssociative();
58
59
        if (empty($documents)) {
60
            return [];
61
        }
62
63
        $results = array_filter($documents, function (array $document) use ($deepLinkCodeRendererConfig) {
64
            if (!empty($deepLinkCodeRendererConfig)) {
65
                return $document['orderVersionId'] === $document['versionId']
66
                    && $deepLinkCodeRendererConfig === $document['deepLinkCode'];
67
            }
68
69
            return $document['orderVersionId'] === $document['versionId'];
70
        });
71
72
        // Set the order version ID to LIVE_VERSION if no matching documents were found
73
        $documents[0]['orderVersionId'] = Defaults::LIVE_VERSION;
74
75
        // Return the first document from the filtered results, or the first document if no filter was applied
76
        return empty($results) ? $documents[0] : reset($results);
77
    }
78
}
79