Passed
Push — master ( 61ffee...578120 )
by Aleksandr
13:14
created

ExportManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 40
ccs 22
cts 22
cp 1
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriver() 0 3 1
A run() 0 18 1
A setDriver() 0 3 1
A hasDriver() 0 3 1
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 * @author    Aleksandr Drobotik <[email protected]>
5
 * @copyright 2023 Aleksandr Drobotik
6
 * @license   https://opensource.org/license/mit  The MIT License
7
 */
8
declare(strict_types=1);
9
10
namespace Drobotik\Eav\Export;
11
12
use Drobotik\Eav\Enum\_ENTITY;
13
use Drobotik\Eav\Driver;
14
use Drobotik\Eav\QueryBuilder\Config;
15
use Drobotik\Eav\QueryBuilder\QueryBuilder;
16
use Drobotik\Eav\Result\Result;
17
use Drobotik\Eav\Trait\DomainTrait;
18
use Drobotik\Eav\Trait\SingletonsTrait;
19
20
class ExportManager
21
{
22
    use DomainTrait;
23
    use SingletonsTrait;
24
25
    private Driver $driver;
26
27 1
    public function setDriver(Driver $driver): void
28
    {
29 1
        $this->driver = $driver;
30
    }
31
32 1
    public function getDriver(): Driver
33
    {
34 1
        return $this->driver;
35
    }
36
37 1
    public function hasDriver(): bool
38
    {
39 1
        return isset($this->driver);
40
    }
41
42 1
    public function run(int $domainKey, int $setKey, array $filters, array $columns): Result
43
    {
44 1
        $qb = new QueryBuilder();
45 1
        $config = new Config();
46 1
        $config->setDomainKey($domainKey);
47 1
        $config->setSetKey($setKey);
48 1
        $config->addAttributes($this->makeAttributeSetModel()->findAttributes($domainKey, $setKey));
49 1
        $config->addColumns($columns);
50 1
        $config->parse($filters);
51 1
        $qb->setConfig($config);
52 1
        $records = $qb->run()
53 1
            ->executeQuery()
54 1
            ->fetchAllAssociative();
55
56 1
        $header = array_merge([_ENTITY::ID->column()], $columns);
57 1
        $driver = $this->getDriver();
58 1
        $driver->setHeader($header);
59 1
        return $driver->writeAll($records);
60
    }
61
62
63
}