StaticSitePageTransformer::transform()   C
last analyzed

Complexity

Conditions 12
Paths 79

Size

Total Lines 81
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 81
rs 6.9666
cc 12
nc 79
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpTek\Exodus\Transform;
4
5
use SilverStripe\Core\Injector\Injectable;
6
use SilverStripe\Versioned\Versioned;
7
use PhpTek\Exodus\Transform\StaticSiteDataTypeTransformer;
8
9
/**
10
 * URL transformer specific to SilverStripe's `SiteTree` class for use with the module's
11
 * import content feature.
12
 * If enabled in the CMS UI, links to imported pages will be automatically re-written.
13
 *
14
 * @package phptek/silverstripe-exodus
15
 * @author Sam Minee <[email protected]>
16
 * @author Russell Michell <[email protected]>
17
 * @see {@link StaticSiteDataTypeTransformer}
18
 */
19
class StaticSitePageTransformer extends StaticSiteDataTypeTransformer
20
{
21
    /**
22
     *
23
     * @var string
24
     */
25
    private static $import_root = 'import-home';
26
27
    /**
28
     * Default value to pass to usleep() to reduce load on the remote server
29
     *
30
     * @var number
31
     */
32
    private static $sleep_multiplier = 100;
33
34
    /**
35
     *
36
     * @return void
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->setParentId(1);
42
    }
43
44
    /**
45
     * Generic function called by \ExternalContentImporter
46
     *
47
     * @param StaticSiteContentItem $item
0 ignored issues
show
Bug introduced by
The type PhpTek\Exodus\Transform\StaticSiteContentItem 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...
48
     * @param mixed SilverStripe\ORM\DataObject|null $parentObject
0 ignored issues
show
Bug introduced by
The type PhpTek\Exodus\Transform\...erStripe\ORM\DataObject was not found. Did you mean SilverStripe\ORM\DataObject? If so, make sure to prefix the type with \.
Loading history...
49
     * @param string $strategy
50
     * @return mixed StaticSiteTransformResult | boolean
51
     * @throws \Exception
52
     */
53
    public function transform($item, $parentObject, $strategy)
54
    {
55
        $this->utils->log("START page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME);
56
57
        if (!$item->checkIsType('sitetree')) {
58
            $this->utils->log(" - Item not of type \'sitetree\'. for: ", $item->AbsoluteURL, $item->ProcessedMIME);
59
            $this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME);
60
61
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by ExternalContentTransformer::transform() of TransformResult.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
62
        }
63
64
        $source = $item->getSource();
65
        // Sleep to reduce load on the remote server
66
        usleep((int) self::$sleep_multiplier * 1000);
67
        // Extract content from the page
68
        $contentFields = $this->getContentFieldsAndSelectors($item, 'SiteTree');
69
70
        // Default value for Title
71
        if (is_array($contentFields)) {
72
            if (empty($contentFields['Title'])) {
73
                $contentFields['Title'] = ['content' => $item->Name];
74
            }
75
76
            // Default value for URLSegment
77
            if (empty($contentFields['URLSegment'])) {
78
                // $item->Name comes from StaticSiteContentItem::init() and is a URL
79
                $name = ($item->Name == '/' ? self::$import_root : $item->Name);
80
                $urlSegment = preg_replace('#\.[^.]*$#', '', $name); // Lose file-extensions e.g .html
81
                $contentFields['URLSegment'] = ['content' => $urlSegment];
82
            }
83
84
            // Default value for Content (Useful for during unit-testing)
85
            if (empty($contentFields['Content'])) {
86
                $contentFields['Content'] = ['content' => 'No content found'];
87
                $this->utils->log(" - No content found for 'Content' field.", $item->AbsoluteURL, $item->ProcessedMIME);
88
            }
89
        }
90
91
        // Get a user-defined schema suited to this URL and Mime
92
        $schema = $source->getSchemaForURL($item->AbsoluteURL, $item->ProcessedMIME);
93
94
        if (!$schema) {
95
            $this->utils->log(" - Couldn't find an import schema for: ", $item->AbsoluteURL, $item->ProcessedMIME);
96
            $this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME);
97
98
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by ExternalContentTransformer::transform() of TransformResult.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
99
        }
100
101
        // TODO Exception vs return false??
102
        if (!$schema->DataType) {
103
            $this->utils->log(" - DataType for migration schema is empty for: ", $item->AbsoluteURL, $item->ProcessedMIME);
104
            $this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME);
105
            throw new \Exception('DataType for migration schema is empty!');
106
        }
107
108
        // Process incoming according to user-selected duplication strategy
109
        if (!$page = $this->duplicationStrategy($schema->DataType, $item, $source->BaseUrl, $strategy, $parentObject)) {
110
            $this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME);
111
112
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by ExternalContentTransformer::transform() of TransformResult.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
113
        }
114
115
        $page->StaticSiteContentSourceID = $source->ID;
116
        $page->StaticSiteURL = $item->AbsoluteURL;
117
        $page->StaticSiteImportID = $this->getCurrentImportID();
118
        $page->Status = 'Published';
119
120
        foreach ($contentFields as $property => $map) {
121
            // Don't write anything new, if we have nothing new to write (useful during unit-testing)
122
            if (!empty($map['content'])) {
123
                $page->$property = $map['content'];
124
            }
125
        }
126
127
        Versioned::set_reading_mode('Stage.Stage');
128
        $page->write();
129
        $page->publishRecursive();
130
131
        $this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME);
132
133
        return StaticSiteTransformResult::create($page, $item->stageChildren());
134
    }
135
}
136