PublisherDelete   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 75
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A displayDeletable() 0 6 2
A doDelete() 0 5 2
A listDeletable() 0 9 5
A handle() 0 13 3
A __construct() 0 8 1
1
<?php namespace Todaymade\Daux\Format\Confluence;
2
3
class PublisherDelete
4
{
5
    /**
6
     * @var \Symfony\Component\Console\Output\OutputInterface
7
     */
8
    public $output;
9
10
    /**
11
     * @var array files that can be deleted
12
     */
13
    protected $deletable;
14
15
    /**
16
     * @var boolean should delete ?
17
     */
18
    protected $delete;
19
20
    /**
21
     * @var Api
22
     */
23
    protected $client;
24
25
    public function __construct($output, $delete, $client)
26
    {
27
        $this->output = $output;
28
        $this->delete = $delete;
29
        $this->client = $client;
30
31
32
        $this->deletable = [];
33
    }
34
35
    protected function listDeletable($published, $prefix = '')
36
    {
37
        foreach ($published['children'] as $child) {
38
            if (array_key_exists('children', $child) && count($child['children'])) {
39
                $this->listDeletable($child, $child['title'] . '/');
40
            }
41
42
            if (!array_key_exists('needed', $child)) {
43
                $this->deletable[$child['id']] = $prefix . $child['title'];
44
            }
45
        }
46
    }
47
48
    public function handle($published)
49
    {
50
        $this->listDeletable($published);
51
52
        if (!count($this->deletable)) {
53
            return;
54
        }
55
56
        if ($this->delete) {
57
            $this->doDelete();
58
59
        } else {
60
            $this->displayDeletable();
61
        }
62
    }
63
64
    protected function doDelete() {
65
        $this->output->writeLn('Deleting obsolete pages...');
66
        foreach ($this->deletable as $id => $title) {
67
            $this->output->writeLn("- $title");
68
            $this->client->deletePage($id);
69
        }
70
    }
71
72
    protected function displayDeletable() {
73
        $this->output->writeLn('Listing obsolete pages...');
74
        $this->output->writeLn("> The following pages will not be deleted, but just listed for information.");
75
        $this->output->writeLn("> If you want to delete these pages, you need to set the --delete flag on the command.");
76
        foreach ($this->deletable as $id => $title) {
77
            $this->output->writeLn("- $title");
78
        }
79
    }
80
}
81