| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace PhpTek\Exodus\Model; |
||||
| 4 | |||||
| 5 | use ExternalContentItem; |
||||
| 6 | use ExternalContentTransformer; |
||||
| 7 | use PhpTek\Exodus\Transform\StaticSiteFileTransformer; |
||||
| 8 | use PhpTek\Exodus\Transform\StaticSitePageTransformer; |
||||
| 9 | use PhpTek\Exodus\Tool\StaticSiteMimeProcessor; |
||||
| 10 | use PhpTek\Exodus\Tool\StaticSiteUtils; |
||||
| 11 | use SilverStripe\Core\Convert; |
||||
| 12 | use SilverStripe\ORM\ArrayList; |
||||
| 13 | use SilverStripe\Forms\ReadonlyField; |
||||
| 14 | use SilverStripe\ORM\Hierarchy\Hierarchy; |
||||
| 15 | use SilverStripe\View\Requirements; |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * Deals-to transforming imported SiteTree and File objects |
||||
| 19 | * |
||||
| 20 | * @package phptek/silverstripe-exodus |
||||
| 21 | * @author Sam Minee <[email protected]> |
||||
| 22 | * @author Russell Michell <[email protected]> |
||||
| 23 | */ |
||||
| 24 | class StaticSiteContentItem extends ExternalContentItem |
||||
| 25 | { |
||||
| 26 | /** |
||||
| 27 | * @var string |
||||
| 28 | */ |
||||
| 29 | private static $table_name = 'StaticSiteContentItem'; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * Default Content type, either 'sitetree', 'file' (or false to disable the default) |
||||
| 33 | * |
||||
| 34 | * @var mixed (string | boolean) |
||||
| 35 | */ |
||||
| 36 | private $default_content_type = 'sitetree'; |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * @var array |
||||
| 40 | */ |
||||
| 41 | private static $extensions = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 42 | 'hierarchy' => Hierarchy::class, |
||||
| 43 | ]; |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * @return void |
||||
| 47 | */ |
||||
| 48 | public function init() |
||||
| 49 | { |
||||
| 50 | $url = $this->externalId; |
||||
| 51 | $processedURL = $this->source->urlList()->processedURL($url); |
||||
| 52 | $parentURL = $this->source->urlList()->parentProcessedURL($processedURL); |
||||
| 53 | $subURL = substr( |
||||
| 54 | $processedURL ? $processedURL['url'] : '', |
||||
| 55 | strlen($parentURL['url']) |
||||
| 56 | ); |
||||
| 57 | |||||
| 58 | if ($subURL != '/') { |
||||
| 59 | $subURL = trim($subURL, '/'); |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | // Just default values |
||||
| 63 | $this->Name = $subURL; |
||||
|
0 ignored issues
–
show
|
|||||
| 64 | $this->Title = $this->Name; |
||||
| 65 | $this->AbsoluteURL = rtrim($this->source->BaseUrl, '/') . $this->externalId; |
||||
|
0 ignored issues
–
show
It seems like
$this->source->BaseUrl can also be of type null; however, parameter $string of rtrim() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 66 | $this->ProcessedURL = $processedURL['url'] ?? ''; |
||||
|
0 ignored issues
–
show
|
|||||
| 67 | $this->ProcessedMIME = $processedURL['mime'] ?? ''; |
||||
|
0 ignored issues
–
show
|
|||||
| 68 | } |
||||
| 69 | |||||
| 70 | /** |
||||
| 71 | * |
||||
| 72 | * @param boolean $showAll |
||||
| 73 | * @return ArrayList |
||||
| 74 | */ |
||||
| 75 | public function stageChildren($showAll = false) |
||||
| 76 | { |
||||
| 77 | if (!$this->source->urlList()->hasCrawled()) { |
||||
| 78 | return ArrayList::create(); |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | $childrenURLs = $this->source->urlList()->getChildren($this->externalId); |
||||
| 82 | $children = ArrayList::create(); |
||||
| 83 | |||||
| 84 | foreach ($childrenURLs as $child) { |
||||
| 85 | $children->push($this->source->getObject($child)); |
||||
| 86 | } |
||||
| 87 | |||||
| 88 | return $children; |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * |
||||
| 93 | * @return int |
||||
| 94 | */ |
||||
| 95 | public function numChildren(): int |
||||
| 96 | { |
||||
| 97 | if (!$this->source->urlList()->hasCrawled()) { |
||||
| 98 | return 0; |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | return count($this->source->urlList()->getChildren($this->externalId)); |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * Returns the correct lowercase Silverstripe base class-name based on the current URL's Mime-Type |
||||
| 106 | * and directs the module to use the correct StaticSiteDataTypeTransformer subclass. |
||||
| 107 | * |
||||
| 108 | * @return mixed string |
||||
| 109 | */ |
||||
| 110 | public function getType(): string |
||||
| 111 | { |
||||
| 112 | $mimeTypeProcessor = singleton(StaticSiteMimeProcessor::class); |
||||
| 113 | |||||
| 114 | if ($mimeTypeProcessor->isOfFileOrImage($this->ProcessedMIME)) { |
||||
| 115 | return 'file'; |
||||
| 116 | } |
||||
| 117 | |||||
| 118 | if ($mimeTypeProcessor->isOfHtml($this->ProcessedMIME)) { |
||||
| 119 | return 'sitetree'; |
||||
| 120 | } |
||||
| 121 | |||||
| 122 | // Log everything that doesn't fit: |
||||
| 123 | singleton(StaticSiteUtils::class)->log('UNKNOWN Schema not configured for Mime & URL:', $this->AbsoluteURL, $this->ProcessedMIME); |
||||
| 124 | |||||
| 125 | return $this->default_content_type; |
||||
| 126 | } |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * Returns the correct content-object transformation class. |
||||
| 130 | * |
||||
| 131 | * @return ExternalContentTransformer |
||||
| 132 | */ |
||||
| 133 | public function getTransformer(): ExternalContentTransformer |
||||
| 134 | { |
||||
| 135 | $type = $this->getType(); |
||||
| 136 | |||||
| 137 | if ($type == 'file') { |
||||
| 138 | return StaticSiteFileTransformer::create(); |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | if ($type == 'sitetree') { |
||||
| 142 | return StaticSitePageTransformer::create(); |
||||
| 143 | } |
||||
|
0 ignored issues
–
show
The function implicitly returns
null when the if condition on line 141 is false. This is incompatible with the type-hinted return ExternalContentTransformer. Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
Loading history...
|
|||||
| 144 | } |
||||
| 145 | |||||
| 146 | /** |
||||
| 147 | * @return FieldList $fields |
||||
|
0 ignored issues
–
show
The type
PhpTek\Exodus\Model\FieldList 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 148 | */ |
||||
| 149 | public function getCMSFields() |
||||
| 150 | { |
||||
| 151 | $fields = parent::getCMSFields(); |
||||
| 152 | |||||
| 153 | // Add the preview fields here, including rules used |
||||
| 154 | $urlField = ReadonlyField::create( |
||||
| 155 | "PreviewSourceURL", |
||||
| 156 | "Imported from", |
||||
| 157 | "<a href=\"$this->AbsoluteURL\">" . Convert::raw2xml($this->AbsoluteURL) . "</a>" |
||||
|
0 ignored issues
–
show
Are you sure
SilverStripe\Core\Conver...xml($this->AbsoluteURL) of type array|string can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 158 | ); |
||||
| 159 | $urlField->dontEscape = true; |
||||
| 160 | $fields->addFieldToTab("Root.Preview", $urlField); |
||||
| 161 | |||||
| 162 | $dataType = $this->getType(); |
||||
| 163 | $content = ''; |
||||
| 164 | |||||
| 165 | if ($t = $this->getTransformer()) { |
||||
| 166 | $content = $t->getContentFieldsAndSelectors($this, $dataType); |
||||
| 167 | } |
||||
| 168 | |||||
| 169 | if (count($content) === 0) { |
||||
| 170 | return $fields; |
||||
|
0 ignored issues
–
show
|
|||||
| 171 | } |
||||
| 172 | |||||
| 173 | foreach ($content as $k => $v) { |
||||
| 174 | $readonlyField = ReadonlyField::create("Preview$k", "$k<br>\n<em>" . $v['selector'] . "</em>", $v['content']); |
||||
| 175 | $readonlyField->addExtraClass('readonly-click-toggle'); |
||||
| 176 | $fields->addFieldToTab("Root.Preview", $readonlyField); |
||||
| 177 | } |
||||
| 178 | |||||
| 179 | Requirements::javascript('phptek/silverstripe-exodus:js/StaticSiteContentItem.js'); |
||||
| 180 | |||||
| 181 | return $fields; |
||||
|
0 ignored issues
–
show
|
|||||
| 182 | } |
||||
| 183 | |||||
| 184 | /** |
||||
| 185 | * Performs some checks on $item. If it is of the wrong type, returns false |
||||
| 186 | * |
||||
| 187 | * @param string $type e.g. 'sitetree' |
||||
| 188 | * @return boolean |
||||
| 189 | */ |
||||
| 190 | public function checkIsType(string $type): bool |
||||
| 191 | { |
||||
| 192 | return $this->getType() === strtolower($type); |
||||
| 193 | } |
||||
| 194 | } |
||||
| 195 |