Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
GroupOlxFeedBuilder
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 build
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
3
 buildFromListing
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Publishing\Connectors\GroupOlx\Application\Services;
6
7use ImovelZapAi\Publishing\Application\Services\VrSyncHeaderGenerator;
8use ImovelZapAi\Publishing\Connectors\GroupOlx\Application\Data\GroupOlxFeedBuildResultData;
9use ImovelZapAi\Publishing\Connectors\GroupOlx\Application\Data\GroupOlxMappedListingData;
10use ImovelZapAi\Publishing\Connectors\GroupOlx\Application\Mappers\GroupOlxListingMapper;
11use ImovelZapAi\Publishing\Connectors\GroupOlx\Application\Ports\GroupOlxFeedBuilderInterface;
12use ImovelZapAi\Publishing\Connectors\GroupOlx\Domain\Exceptions\GroupOlxMappingException;
13use ImovelZapAi\Publishing\Domain\Canonical\NormalizedListing;
14use ImovelZapAi\Publishing\Domain\Feeds\VrSync\ListingDataFeed;
15
16/**
17 * NormalizedListing[] → XML VRSync (via mapper + builder).
18 *
19 * Não persiste e não valida — isso fica na action/job.
20 */
21final class GroupOlxFeedBuilder implements GroupOlxFeedBuilderInterface
22{
23    public function __construct(
24        private readonly GroupOlxListingMapper $listingMapper,
25        private readonly VrsyncXmlBuilder $xmlBuilder,
26        private readonly VrSyncHeaderGenerator $headerGenerator,
27    ) {}
28
29    public function build(array $listings): GroupOlxFeedBuildResultData
30    {
31        if ($listings === []) {
32            throw GroupOlxMappingException::withErrors(['Nenhum imóvel canônico para gerar o feed.']);
33        }
34
35        $mapped = [];
36        $codes = [];
37
38        foreach ($listings as $listing) {
39            $item = $this->listingMapper->map($listing);
40            $mapped[] = $item;
41            $codes[] = $item->listingId();
42        }
43
44        $header = $this->headerGenerator->generate($listings[0]);
45        $xml = $this->xmlBuilder->buildFromMapped($header, $mapped);
46
47        return new GroupOlxFeedBuildResultData(
48            feed: new ListingDataFeed(
49                header: $header,
50                listings: array_map(
51                    static fn (GroupOlxMappedListingData $item): \ImovelZapAi\Publishing\Domain\Feeds\VrSync\ListingDataFeedListing => $item->listing,
52                    $mapped,
53                ),
54            ),
55            xml: $xml,
56            listingCodes: $codes,
57        );
58    }
59
60    public function buildFromListing(NormalizedListing $listing): GroupOlxFeedBuildResultData
61    {
62        return $this->build([$listing]);
63    }
64}