SitePublisherTask   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 52 8
1
<?php
2
3
namespace LeKoala\DevToolkit\Tasks;
4
5
use LeKoala\DevToolkit\BuildTaskTools;
6
use SilverStripe\Dev\BuildTask;
7
use SilverStripe\Versioned\Versioned;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Versioned\Versioned was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
/**
10
 */
11
class SitePublisherTask extends BuildTask
12
{
13
    use BuildTaskTools;
14
15
    protected $title = "Site Publisher";
16
    protected $description = 'Publish the whole site in one click.';
17
    private static $segment = 'SitePublisherTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
18
19
    public function run($request)
20
    {
21
        $this->request = $request;
22
        $this->addOption("classes", "Classes to publish (comma separated)", 'Page');
23
        $this->addOption("go", "Set this to 1 to proceed", 0);
24
25
        $options = $this->askOptions();
26
27
        $classes = $options['classes'];
28
        $go = $options['go'];
29
30
        $cbSave = function ($List, $publish = false) {
31
            foreach ($List as $Item) {
32
                $this->message('Saving item "' . $Item->getTitle() . '"');
33
                if ($publish) {
34
                    $Item->publishRecursive();
35
                } else {
36
                    $Item->write();
37
                }
38
            }
39
        };
40
41
        $classesToPublish = explode(',', $classes);
42
43
        if ($go) {
44
            foreach ($classesToPublish as $class) {
45
                $this->message("Publishing class $class");
46
47
                $List = $class::get();
48
                $singl = $class::singleton();
49
                $fluent = $singl->has_extension("\\TractorCow\\Fluent\\Extension\\FluentExtension");
50
51
                $publish = $singl->has_extension(Versioned::class);
52
53
                // With fluent we need to change state when saving
54
                if ($fluent) {
55
                    $state = \TractorCow\Fluent\State\FluentState::singleton();
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\State\FluentState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
                    $allLocales = \TractorCow\Fluent\Model\Locale::get();
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\Model\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
                    foreach ($allLocales as $locale) {
58
                        $this->message('Publishing with locale ' . $locale->Locale, "info");
59
                        $state->withState(function ($state) use ($locale, $List, $cbSave, $publish) {
60
                            $state->setLocale($locale->Locale);
61
                            $cbSave($List, $publish);
62
                        });
63
                    }
64
                } else {
65
                    $cbSave($List, $publish);
66
                }
67
            }
68
        } else {
69
            foreach ($classesToPublish as $class) {
70
                $this->message("Would publish " . $class::get()->count() . " items for class " . $class);
71
            }
72
        }
73
    }
74
}
75