Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

LocateContent   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A process() 0 20 3
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Step;
10
11
use Cecil\Exception\Exception;
12
use Symfony\Component\Finder\Finder;
13
14
/**
15
 * Locates content.
16
 */
17
class LocateContent extends AbstractStep
18
{
19
    /**
20
     * {@inheritdoc}
21
     *
22
     * @throws Exception
23
     */
24
    public function init($options)
25
    {
26
        if (!is_dir($this->builder->getConfig()->getContentPath())) {
27
            throw new Exception(sprintf('%s not found!', $this->builder->getConfig()->getContentPath()));
28
        }
29
30
        $this->process = true;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function process()
37
    {
38
        call_user_func_array($this->builder->getMessageCb(), ['LOCATE', 'Loading content']);
39
40
        try {
41
            $content = Finder::create()
42
                ->files()
43
                ->in($this->builder->getConfig()->getContentPath())
44
                ->name('/\.('.implode('|', $this->builder->getConfig()->get('content.ext')).')$/');
45
            if (!$content instanceof Finder) {
46
                throw new Exception(__FUNCTION__.': result must be an instance of Symfony\Component\Finder.');
47
            }
48
            $count = $content->count();
49
            call_user_func_array($this->builder->getMessageCb(), ['LOCATE_PROGRESS', 'Start locating', 0, $count]);
50
            $this->builder->setContent($content);
51
            call_user_func_array($this->builder->getMessageCb(), ['LOCATE_PROGRESS', 'Files loaded', $count, $count]);
52
        } catch (Exception $e) {
53
            echo $e->getMessage()."\n";
54
        }
55
    }
56
}
57