Skip to content

Commit

Permalink
refactoring phase one
Browse files Browse the repository at this point in the history
  • Loading branch information
secure73 committed Sep 26, 2024
1 parent f78baf9 commit 0c931d6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
"minimum-stability": "stable",
"require-dev": {
"pestphp/pest": "^2.34"
"pestphp/pest": "^2.34",
"phpstan/phpstan": "^1.12"
},
"config": {
"allow-plugins": {
Expand Down
13 changes: 5 additions & 8 deletions src/core/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ public function __construct(Request $request,array $arrayRolesToAuthorize = null
}
if(is_array($arrayRolesToAuthorize) && count($arrayRolesToAuthorize) )
{
if(!$this->token)
if(!$this->authorize($arrayRolesToAuthorize))
{
Response::forbidden("there is no valid Token or Token does not exists in header")->show();
die;
}
if(!$this->authorize($arrayRolesToAuthorize) || !$this->token)
{
Response::unauthorized("role {$this->token->role} is not allowed to perform this action")->show();
// @phpstan-ignore-next-line
Response::unauthorized("role {$this?->token?->role} is not allowed to perform this action")->show();
die;
}
}
Expand All @@ -71,6 +67,7 @@ public function getUserId():int|null
*/
private function authorize(array $roles): bool
{
// @phpstan-ignore-next-line
if (!in_array($this->token->role, $roles)) {
return false;
}
Expand Down Expand Up @@ -106,7 +103,7 @@ private function authenticate(): bool
}
$existed_token = $this->request->getJwtToken();

if (!$existed_token->verify()) {
if (!$existed_token || !$existed_token->verify()) {
return false;
}
$this->token = $existed_token;
Expand Down
5 changes: 3 additions & 2 deletions src/core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ public function mapPostManuel(array $postNames , Table $object): bool
public function createSingle(Table $object):JsonResponse
{
$this->mapPost($object);
/**@phpstan-ignore-next-line*/
// @phpstan-ignore-next-line
$result_id = $object->insertSingleQuery();
if(!$result_id)
{
return Response::internalError($object->getError());
}
// @phpstan-ignore-next-line
$object->id = $result_id;
return Response::created($object,1,'created successfully');
}
Expand All @@ -102,8 +103,8 @@ public function updateSingle(Table $object):JsonResponse
{
return Response::notAcceptable('post id is required');
}
// @phpstan-ignore-next-line
$id = (int)$this->request->post['id'];
/**@phpstan-ignore-next-line*/
$find_object = $object->select()->where('id',$id)->limit(1)->run();
if(!is_array($find_object) || count($find_object) !== 1)
{
Expand Down
17 changes: 12 additions & 5 deletions src/core/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@

class GemvcRunner
{
/**
* @var array<mixed> $commands
*/
private $commands = [];

public function __construct()
{
$this->registerDefaultCommands();
}

private function registerDefaultCommands()
private function registerDefaultCommands():void
{
$this->commands['Migrate'] = [$this, 'handleMigrate'];
}

public function run($argv)
public function run($argv):void
{
if (count($argv) < 3) {
echo "Usage: Gemvc <Command> <ClassName>\n";
Expand All @@ -33,8 +36,12 @@ public function run($argv)

call_user_func($this->commands[$command], $className);
}

private function handleMigrate($className)
/**
* Summary of handleMigrate
* @param string $className
* @return void
*/
private function handleMigrate($className):void
{
if (!class_exists($className)) {
echo "Class '$className' not found.\n";
Expand All @@ -53,7 +60,7 @@ private function handleMigrate($className)
}
}

private function printAvailableCommands()
private function printAvailableCommands():void
{
echo "Available commands:\n";
foreach (array_keys($this->commands) as $command) {
Expand Down
5 changes: 2 additions & 3 deletions src/core/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class Table extends PdoQuery
{
private ?string $_query;
private bool $_isSelectSet;

private bool $_no_limit;
/**
* @var array<mixed> $_binds
*/
private bool $_no_limit;
private array $_binds;
private int $_limit;
private int $_offset;
Expand Down Expand Up @@ -242,12 +243,10 @@ public function run(): false|array
return [];
}
$object_result = [];
/**@phpstan-ignore-next-line */
$this->_total_count = $queryResult[0]['_total_count'];
/**@phpstan-ignore-next-line */
$this->_count_pages = round($this->_total_count / $this->_limit);
foreach ($queryResult as $item) {
/**@phpstan-ignore-next-line */
unset($item['_total_count']);
$instance = new $this();
if (is_array($item)) {
Expand Down
8 changes: 7 additions & 1 deletion src/core/TableGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ public function createTableFromObject(Table $object) :bool{
return false;
}

private function getPropertyType($object, $property) {
/**
* Summary of getPropertyType
* @param mixed $object
* @param string $property
* @return string
*/
private function getPropertyType($object, $property):string {
$type = gettype($object->$property);
if ($type === 'object' && get_class($object->$property) === 'DateTime') {
return 'DateTime';
Expand Down

0 comments on commit 0c931d6

Please sign in to comment.