Passed
Push — master ( 08f92a...936030 )
by Dev
13:44
created

PageScannerCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 78
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A configure() 0 5 1
A scanAll() 0 21 4
A execute() 0 11 2
A scanAllWithLock() 0 13 2
1
<?php
2
3
namespace PiedWeb\CMSBundle\Extension\PageScanner;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Filesystem\Filesystem;
11
use Symfony\Component\Lock\LockFactory;
12
use Symfony\Component\Lock\Store\FlockStore;
13
14
class PageScannerCommand extends Command
15
{
16
    private $filesystem;
17
    private $scanner;
18
    private $pageClass;
19
    private $em;
20
21
    public function __construct(
22
        PageScannerService $scanner,
23
        Filesystem $filesystem,
24
        EntityManagerInterface $em,
25
        string $pageClass,
26
        string $varDir
27
    ) {
28
        parent::__construct();
29
        $this->scanner = $scanner;
30
        $this->pageClass = $pageClass;
31
        $this->em = $em;
32
        $this->filesystem = $filesystem;
33
        PageScannerController::$fileCache = $varDir.PageScannerController::$fileCache;
34
    }
35
36
    protected function configure()
37
    {
38
        $this
39
            ->setName('page:scan')
40
            ->addArgument('host', InputArgument::OPTIONAL);
41
    }
42
43
    protected function scanAllWithLock()
44
    {
45
        $lock = (new LockFactory(new FlockStore()))->createLock('page-scan');
46
        if ($lock->acquire()) {
47
            sleep(30);
48
            $errors = $this->scanAll();
49
            $this->filesystem->dumpFile(PageScannerController::$fileCache, serialize($errors));
50
            $lock->release();
51
52
            return true;
53
        }
54
55
        return false;
56
    }
57
58
    protected function scanAll()
59
    {
60
        $pages = $this->em->getRepository($this->pageClass)->findAll();
61
62
        $errors = [];
63
        $errorNbr = 0;
64
65
        foreach ($pages as $page) {
66
            // todo import scanner via setScanner + services.yaml
67
            $scan = $this->scanner->scan($page);
68
            if (true !== $scan) {
69
                $errors[$page->getId()] = $scan;
70
                $errorNbr = $errorNbr + \count($errors[$page->getId()]);
71
            }
72
73
            if ($errorNbr > 500) {
74
                break;
75
            }
76
        }
77
78
        return $errors;
79
    }
80
81
    protected function execute(InputInterface $input, OutputInterface $output)
82
    {
83
        //if ($input->getArgument('host'))
84
85
        if ($this->scanAllWithLock()) {
86
            $output->writeln('done...');
87
        } else {
88
            $output->writeln('cannot acquire the lock...');
89
        }
90
91
        return 0;
92
    }
93
}
94