Completed
Push — 16.x ( 0056bb...bd3602 )
by Tim
02:11 queued 10s
created

CreateOkFilesPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.9
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Plugins\CreateOkFilesPlugin
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2020 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Plugins;
22
23
use TechDivision\Import\ApplicationInterface;
24
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
25
use TechDivision\Import\Subjects\FileWriter\FileWriterFactoryInterface;
26
27
/**
28
 * Plugin that creates .OK files for the .CSV files found in the actual source directory.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2020 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
class CreateOkFilesPlugin extends AbstractPlugin
37
{
38
39
    /**
40
     * The file writer factory instance.
41
     *
42
     * @var \TechDivision\Import\Subjects\FileWriter\FileWriterFactoryInterface
43
     */
44
    protected $fileWriterFactory;
45
46
    /**
47
     *
48
     * @param \TechDivision\Import\ApplicationInterface                           $application       The application instance
49
     * @param \TechDivision\Import\Subjects\FileWriter\FileWriterFactoryInterface $fileWriterFactory The file writer factory instance
50
     */
51
    public function __construct(
52
        ApplicationInterface $application,
53
        FileWriterFactoryInterface $fileWriterFactory
54
    ) {
55
56
        // set the passed file writer factory instance
57
        $this->fileWriterFactory = $fileWriterFactory;
58
59
        // pass the application to the parent constructor
60
        parent::__construct($application);
61
    }
62
63
    /**
64
     * Return's the file writer factory instance.
65
     *
66
     * @return \TechDivision\Import\Subjects\FileWriter\FileWriterFactoryInterface The file writer factory instance
67
     */
68
    protected function getFileWriterFactory() : FileWriterFactoryInterface
69
    {
70
        return $this->fileWriterFactory;
71
    }
72
73
    /**
74
     * Process the plugin functionality.
75
     *
76
     * @return void
77
     * @throws \Exception Is thrown, if the plugin can not be processed
78
     */
79
    public function process()
80
    {
81
82
        // initialize the counter for the CSV files
83
        $okFilesCreated = 1;
84
85
        // initialize the subject filter we want to use
86
        $filters = array(function (SubjectConfigurationInterface $subject) {
87
            return $subject->isOkFileNeeded();
88
        });
89
90
        // load the matching subjects (only that one, that needs an .OK file)
91
        $subjects = $this->getConfiguration()->getSubjects($filters);
92
93
        // create the .OK files for that subjects
94
        foreach ($subjects as $subject) {
95
            $okFilesCreated += $this->getFileWriterFactory()->createFileWriter($subject)->createOkFiles($this->getSerial());
96
        }
97
98
        // query whether or not we've found any CSV files
99
        if ($okFilesCreated === 0) {
100
            throw new \Exception(sprintf('Can\'t find any CSV files in source directory "%s"', $this->getSourceDir()));
101
        }
102
    }
103
}
104