DatastoreController::childPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Phpsa\Datastore\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller 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...
6
use Phpsa\Datastore\Datastore;
7
use Phpsa\Datastore\Asset;
8
use Phpsa\Datastore\Helpers;
9
use Phpsa\Datastore\DatastoreException;
10
use Phpsa\Datastore\Repositories\DatastoreRepository;
11
12
use Phpsa\Datastore\Models\Datastore as DatastoreModel;
13
use Phpsa\Datastore\Models\DatastorePages;
14
15
Class DatastoreController extends Controller {
16
17
	/**
18
	 * @var DatastoreRepository
19
	 */
20
    protected $datastoreRepository;
21
22
    /**
23
     * UserController constructor.
24
     *
25
     * @param DatastoreRepository $datastoreRepository
26
     */
27
    public function __construct(DatastoreRepository $datastoreRepository)
28
    {
29
        $this->datastoreRepository = $datastoreRepository;
30
	}
31
32
	protected function iCan($permission, $then, $else){
33
		$user = auth()->user();
34
		return $user && $user->can($permission) ? $then : $else;
35
	}
36
37
	protected function canViewAll($status = 'published'){
38
		return $this->iCan('manage datastore', null , $status);
39
	}
40
41
	protected function getPageBySlug($slug){
42
		$page = DatastorePages::where('slug', $slug)->first();
43
		if(!$page){
44
			abort(404);
45
		}
46
47
		$datastore = $page->datastore;
48
		$user = auth()->user();
49
50
		if(!$datastore->statusIsActive() && (!$user || !$user->can('manage datastore'))) {
0 ignored issues
show
Bug introduced by
The method statusIsActive() does not exist on null. ( Ignorable by Annotation )

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

50
		if(!$datastore->/** @scrutinizer ignore-call */ statusIsActive() && (!$user || !$user->can('manage datastore'))) {

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...
51
			abort(404);
52
		}
53
		return $page;
54
	}
55
56
	public function page($slug){
57
58
		$page = $this->getPageBySlug($slug);
59
60
		$acceptedAssets = $page->datastore->accept ? $this->datastoreRepository->paginateAccepted($page->asset, $this->canViewAll(Helpers::getStatusEquals($page->datastore->accept))) : false;
0 ignored issues
show
Bug Best Practice introduced by
The property accept does not exist on Phpsa\Datastore\Datastore. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
62
		return view($page->datastore->getViewName())
0 ignored issues
show
Bug introduced by
The method getViewName() does not exist on null. ( Ignorable by Annotation )

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

62
		return view($page->datastore->/** @scrutinizer ignore-call */ getViewName())

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...
Bug introduced by
$page->datastore->getViewName() of type void is incompatible with the type string expected by parameter $view of view(). ( Ignorable by Annotation )

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

62
		return view(/** @scrutinizer ignore-type */ $page->datastore->getViewName())
Loading history...
Bug introduced by
Are you sure the usage of $page->datastore->getViewName() targeting Phpsa\Datastore\Datastore::getViewName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
		->withDatastore($page->datastore)
64
		->withChildren($page->datastore->children())
65
		->withAccepted($acceptedAssets)
66
		->withPage($page);
67
68
	}
69
70
	public function childPage($parent_slug, $slug){
71
		return $this->page($slug);
72
	}
73
74
	public function articleByAuthor($author_id, $slug){
75
		$this->datastoreRepository->paginateSearchProp('author', $author_id, Phpsa\Datastore\Ams\Article\ItemAsset::class, $this->canViewAll('published'));
0 ignored issues
show
Bug introduced by
The type Phpsa\Datastore\Http\Con...e\Ams\Article\ItemAsset was not found. Did you mean Phpsa\Datastore\Ams\Article\ItemAsset? If so, make sure to prefix the type with \.
Loading history...
76
	}
77
78
    public function ___tests()
79
    {
80
81
		//ok we need a list of objects we can usse::
82
83
		// $assets = Helpers::getAssetList(true);
84
85
		// echo '<pre>';
86
		// print_r($assets);
87
		// exit;
88
		$tester = Datastore::getAsset(ContentAsset::class);
0 ignored issues
show
Bug introduced by
The type Phpsa\Datastore\Http\Controllers\ContentAsset 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...
89
90
		$tester->prop('title', 'Moe Title');
91
        $tester->prop('content', '<p>Rhoncus hac aliquam aliquam! Et mauris, et quis platea ut elementum natoque sit natoque lectus augue integer aliquam porta rhoncus nec, cursus diam a parturient augue ut! Tincidunt eros urna lacus lorem, sit scelerisque. Proin duis auctor ut. Turpis? Sed, diam elit sed velit dapibus phasellus, pulvinar mattis! Sociis augue in parturient sed ultricies et.</p>');
92
		$tester->prop('status', 'published');
93
94
		//echo '<pre>$tester->val(): '; print_r($tester->val()); echo '</pre>'; die();
95
96
97
		$tester->store();
98
99
100
		echo '<pre>$tester->export(): '; print_r($tester->export()); echo '</pre>'; die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
101
102
103
		/*$tester = Datastore::getAssetById(11);
104
		echo $tester->render('title');
105
106
		echo $tester->form('title');
107
108
		echo $tester->getMetadataForm();
109
110
		echo $tester->getForm();
111
112
113
		print_r($tester->getFieldValues());*/
114
115
		/*
116
		$tester = Datastore::getAsset(ContentAsset::class);
117
118
        $tester->prop('content', '<p>Rhoncus hac aliquam aliquam! Et mauris, et quis platea ut elementum natoque sit natoque lectus augue integer aliquam porta rhoncus nec, cursus diam a parturient augue ut! Tincidunt eros urna lacus lorem, sit scelerisque. Proin duis auctor ut. Turpis? Sed, diam elit sed velit dapibus phasellus, pulvinar mattis! Sociis augue in parturient sed ultricies et.</p>');
119
		$tester->prop('status', 'published');
120
121
		//$tester->prop('title', 'Moe Title');
122
123
		try {
124
			$tester->validate($tester->getFieldValues());
125
		}catch(\Exception $e){
126
			echo $e->getMessage();
127
			print_r($e->errors());
128
		}
129
130
		$tester->prop('title', 'Moe Title');
131
		try {
132
			$tester->validate($tester->getFieldValues());
133
			echo "Form is valid";
134
		}catch(\Exception $e){
135
			echo $e->getMessage();
136
			print_r($e->errors());
137
		}
138
		(*/
139
140
		/*
141
		if( $this->input->post('page_title') && $this->input->post('page_slug'))
142
		{
143
			$this->load->model('database/datastore_pages_model');
144
145
			$page = $this->datastore_pages_model->get_where(array('asset' => $newasset->id), null, 1);
146
147
			if( ! empty($page))
148
			{
149
				$this->datastore_pages_model->update(array(
150
					'title'	 => $this->input->post('page_title'),
151
					'slug' 	 => $this->input->post('page_slug'),
152
					'asset'	 => $newasset->id
153
				), $page->id);
154
			}
155
			else
156
			{
157
				$this->datastore_pages_model->insert(array(
158
					'title'	 => $this->input->post('page_title'),
159
					'slug' 	 => $this->input->post('page_slug'),
160
					'asset'	 => $newasset->id
161
				));
162
			}
163
		}
164
165
		*/
166
167
	}
168
169
170
171
}