Passed
Push — develop ( 74435c...ead2a0 )
by Richard
15:17
created

SchemaOrg   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
eloc 16
c 3
b 0
f 0
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A schemaOrgScript() 0 9 2
A initSchemaOrg() 0 11 5
A addschemaOrg() 0 9 2
1
<?php
2
3
4
namespace Riclep\Storyblok\Traits;
5
6
7
use Riclep\Storyblok\Page;
8
9
trait SchemaOrg
10
{
11
	/**
12
	 * Automatically called to add a schema to the Page
13
	 */
14
	protected function initSchemaOrg(): void
15
	{
16
		if (method_exists($this, 'schemaOrg')) {
17
			if ($this instanceof Page) {
18
				$page = $this;
19
			} else {
20
				$page = $this->page();
0 ignored issues
show
Bug introduced by
It seems like page() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
				/** @scrutinizer ignore-call */ 
21
    $page = $this->page();
Loading history...
21
			}
22
23
			if ($page && count($this->_componentPath) <= config('storyblok.schema_org_depth')) {
24
				$this->addschemaOrg($page);
25
			}
26
		}
27
28
	}
29
30
	/**
31
	 * Returns the JavaScript JSON-LD string
32
	 *
33
	 * @return string
34
	 */
35
	public function schemaOrgScript(): string
36
	{
37
		$schemaJson = '';
38
39
		foreach ($this->meta()['schema_org'] as $schema) {
0 ignored issues
show
Bug introduced by
It seems like meta() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
		foreach ($this->/** @scrutinizer ignore-call */ meta()['schema_org'] as $schema) {
Loading history...
40
			$schemaJson .= $schema->toScript();
41
		}
42
43
		return $schemaJson;
44
	}
45
46
	/**
47
	 * Adds the schema to the meta of the current page
48
	 *
49
	 * @param $page
50
	 */
51
	protected function addschemaOrg($page): void
52
	{
53
		$currentSchemaOrg = $page->meta('schema_org');
54
55
        if ($schema = $this->schemaOrg()) {
0 ignored issues
show
Bug introduced by
The method schemaOrg() does not exist on Riclep\Storyblok\Traits\SchemaOrg. Did you maybe mean schemaOrgScript()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        if ($schema = $this->/** @scrutinizer ignore-call */ schemaOrg()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            $currentSchemaOrg[] = $schema;
57
        }
58
59
		$page->replaceMeta('schema_org', $currentSchemaOrg ?? []);
60
	}
61
}
62