This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement user put- and delete-routes, parameter converter and error-…
…/response-handling
- Loading branch information
1 parent
36c6400
commit 749e48c
Showing
6 changed files
with
197 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
namespace KCMS\Converter; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
|
||
/** | ||
* Class AbstractConverter | ||
* @package KCMS\Converter | ||
*/ | ||
abstract class AbstractConverter | ||
{ | ||
/** | ||
* @var EntityManager | ||
*/ | ||
protected $entityManager; | ||
|
||
/** | ||
* AbstractConverter constructor. | ||
* @param EntityManager $entityManager | ||
*/ | ||
public function __construct(EntityManager $entityManager) | ||
{ | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
/** | ||
* @param $id | ||
* @return object | ||
*/ | ||
abstract public function convertFromId($id); | ||
|
||
/** | ||
* @param $json | ||
* @return object | ||
*/ | ||
abstract public function convertFromJson($json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
namespace KCMS\Converter; | ||
|
||
/** | ||
* Class ConverterAdapter | ||
* @package KCMS\Converter | ||
*/ | ||
class ConverterAdapter extends AbstractConverter | ||
{ | ||
/** | ||
* @param $id | ||
* @return object | ||
*/ | ||
public function convertFromId($id) | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @param $json | ||
* @return object | ||
*/ | ||
public function convertFromJson($json) | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace KCMS\Converter; | ||
|
||
use KCMS\Models\User; | ||
use Symfony\Component\Routing\Exception\ResourceNotFoundException; | ||
|
||
/** | ||
* Class UserConverter | ||
* @package KCMS\Converter | ||
*/ | ||
class UserConverter extends AbstractConverter | ||
{ | ||
/** | ||
* @param $id | ||
* @return User | ||
* @throws ResourceNotFoundException | ||
*/ | ||
public function convertFromId($id) | ||
{ | ||
$foundUser = $this->entityManager->getRepository('KCMS\Models\User')->find($id); | ||
if ($foundUser === null) { | ||
throw new ResourceNotFoundException('User with id ' . $id . ' could not be found.', 404); | ||
} | ||
|
||
return $foundUser; | ||
} | ||
|
||
/** | ||
* @param $json | ||
* @return User | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function convertFromJson($json) | ||
{ | ||
$decoded = json_decode($json, true); | ||
|
||
$user = User::createUser( | ||
$decoded['username'] ?: null, | ||
$decoded['firstName'] ?: null, | ||
$decoded['lastName'] ?: null, | ||
$decoded['email'] ?: null, | ||
$decoded['password'] ?: null | ||
); | ||
|
||
if ($user === null) { | ||
throw new \InvalidArgumentException('User could not be created, invalid arguments.', 400); | ||
} else { | ||
return $user; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters