Skip to content

Commit

Permalink
Merge pull request #21 from Fabricio872/RCB-15-fix-attribute-config-n…
Browse files Browse the repository at this point in the history
…ot-work-for

RCB-15 Fix attribute config not work for edit
  • Loading branch information
Fabricio872 authored Jan 6, 2022
2 parents 31137be + da63df5 commit 67b157a
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 74 deletions.
9 changes: 2 additions & 7 deletions src/Command/AbstractList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fabricio872\RegisterCommand\Command;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
Expand All @@ -12,7 +13,6 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

abstract class AbstractList extends Command
Expand All @@ -39,8 +39,6 @@ abstract class AbstractList extends Command
protected $limitUsers;
/** @var int */
protected $currentPage = 0;
/** @var NormalizerInterface */
protected $normalizer;
/** @var UserPasswordHasherInterface */
protected $passwordEncoder;
/** @var Reader */
Expand All @@ -59,18 +57,15 @@ public function __construct(
int $tableLimit,
int $maxColWidth,
EntityManagerInterface $em,
NormalizerInterface $normalizer,
UserPasswordHasherInterface $passwordEncoder,
Reader $reader,
ValidatorInterface $validator
) {
$this->userClassName = $userClassName;
$this->tableLimit = $tableLimit;
$this->maxColWidth = $maxColWidth;
$this->em = $em;
$this->normalizer = $normalizer;
$this->passwordEncoder = $passwordEncoder;
$this->reader = $reader;
$this->reader = new AnnotationReader();
$this->validator = $validator;
parent::__construct();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Command/UserListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fabricio872\RegisterCommand\Services\ArrayToTable;
use Fabricio872\RegisterCommand\Services\Ask;
use Fabricio872\RegisterCommand\Services\StaticMethods;
use Fabricio872\RegisterCommand\Services\UserEditor;
use Fabricio872\RegisterCommand\Services\UserEditorInterface;

Expand Down Expand Up @@ -36,7 +37,7 @@ protected function draw(?int $page): int

$userArray = [];
foreach ($this->userList as $user) {
$userArray[] = $this->normalizer->normalize($user);
$userArray[] = StaticMethods::getSerializer()->normalize($user);
}
$objectToTable = new ArrayToTable(
$userArray,
Expand Down Expand Up @@ -81,7 +82,6 @@ protected function getPage($page): ?int
$this->output,
$this->em,
$this->userList,
$this->normalizer,
$this->colWidth,
$this->buildAsk()
);
Expand Down
17 changes: 2 additions & 15 deletions src/Command/UserRegisterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\EntityManagerInterface;
use Fabricio872\RegisterCommand\Services\Ask;
use Fabricio872\RegisterCommand\Services\StaticMethods;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class UserRegisterCommand extends Command
Expand Down Expand Up @@ -91,23 +89,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$data[$property->getName()] = $ask->ask($property->getName());
}

$user = $this->getSerializer()->denormalize($data, $this->userClassName);
$user = StaticMethods::getSerializer()->denormalize($data, $this->userClassName);

$this->em->persist($user);
$this->em->flush();

$this->io->success('User' . $ask->getUserIdentifier() . 'registered.');
return 0;
}

/**
* @return Serializer
*/
private function getSerializer(): Serializer
{
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];

return new Serializer($normalizers, $encoders);
}
}
12 changes: 1 addition & 11 deletions src/Services/ArrayToTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function makeTable(): Table
$table->setHeaders(array_keys($this->array[0]));

$table->setRows(array_map(function ($user) {
return $this->getSerializer()->normalize($user);
return StaticMethods::getSerializer()->normalize($user);
}, $this->array));
$table->setStyle('box');

Expand All @@ -44,14 +44,4 @@ public function getCols(): array
{
return array_keys($this->array[0]);
}

/**
* @return Serializer
*/
private function getSerializer(): Serializer
{
$normalizers = [new UserEntityNormalizer()];

return new Serializer($normalizers);
}
}
15 changes: 3 additions & 12 deletions src/Services/Ask.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,7 @@ public function getUserIdentifier(): string
public function ask(string $propertyName)
{
$userReflection = new \ReflectionClass($this->userClassName);
/** @var ?RegisterCommand $annotation */
$annotation = $this->reader->getPropertyAnnotation($userReflection->getProperty($propertyName), RegisterCommand::class);

if (method_exists($userReflection->getProperty($propertyName), 'getAttributes')) {
$attributes = $userReflection->getProperty($propertyName)->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getName() == RegisterCommand::class) {
$annotation = $attribute->newInstance();
}
}
}
$annotation = StaticMethods::getRegisterCommand($this->userClassName, $propertyName);

if ($annotation == null) {
return null;
Expand Down Expand Up @@ -173,7 +163,7 @@ private function processValue($value, string $annotation)
case 'valueString':
return (string)$value;
case 'valuePassword':
return (string)$this->passwordEncoder->encodePassword(new $this->userClassName(), $value);
return (string)$this->passwordEncoder->hashPassword(new $this->userClassName(), $value);
case 'valueArray':
return (array)$value;
case 'valueInt':
Expand Down Expand Up @@ -201,6 +191,7 @@ function ($annotation) {
if ($annotation instanceof Constraint) {
return $annotation;
}
return null;
},
$this->reader->getPropertyAnnotations(
$userReflection->getProperty($propertyName)
Expand Down
1 change: 0 additions & 1 deletion src/Services/Questions/ListInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class ListInput extends QuestionAbstract

private $sttyMode;
private $stream;
private $values = [];
private $activeList = [];
private $tableExist = false;
private $cursor = 0;
Expand Down
50 changes: 50 additions & 0 deletions src/Services/StaticMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php


namespace Fabricio872\RegisterCommand\Services;

use Doctrine\Common\Annotations\AnnotationReader;
use Fabricio872\RegisterCommand\Annotations\RegisterCommand;
use Fabricio872\RegisterCommand\Serializer\UserEntityNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class StaticMethods
{
private static $serializer;

public static function getRegisterCommand(string $userClass, string $propertyName): ?RegisterCommand
{
$reader = new AnnotationReader();

$userReflection = new \ReflectionClass($userClass);
/** @var ?RegisterCommand $annotation */
$annotation = $reader->getPropertyAnnotation($userReflection->getProperty($propertyName), RegisterCommand::class);

if (method_exists($userReflection->getProperty($propertyName), 'getAttributes')) {
$attributes = $userReflection->getProperty($propertyName)->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getName() == RegisterCommand::class) {
$annotation = $attribute->newInstance();
}
}
}

return $annotation;
}

/**
* @return Serializer
*/
public static function getSerializer(): Serializer
{
if (!isset(self::$serializer)) {
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer(), new UserEntityNormalizer()];

self::$serializer = new Serializer($normalizers, $encoders);
}
return self::$serializer;
}
}
38 changes: 13 additions & 25 deletions src/Services/UserEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
use Doctrine\ORM\Mapping\Id;
use Fabricio872\RegisterCommand\Annotations\RegisterCommand;
use Fabricio872\RegisterCommand\Helpers\StreamableInput;
use Fabricio872\RegisterCommand\Serializer\UserEntityNormalizer;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;

class UserEditor implements UserEditorInterface
{
Expand All @@ -26,8 +23,6 @@ class UserEditor implements UserEditorInterface
private $em;
/** @var array */
private $userList;
/** @var NormalizerInterface */
private $normalizer;
/*** @var int */
private $colWidth;
/** @var array */
Expand All @@ -38,21 +33,23 @@ class UserEditor implements UserEditorInterface
private $tableLines;
/** @var Ask */
private $ask;
/** @var bool|resource */
private $stream;
/** @var string */
private $sttyMode;

public function __construct(
InputInterface $input,
OutputInterface $output,
EntityManagerInterface $em,
array $userList,
NormalizerInterface $normalizer,
int $colWidth,
Ask $ask
) {
$this->input = $input;
$this->output = $output;
$this->em = $em;
$this->userList = $userList;
$this->normalizer = $normalizer;
$this->colWidth = $colWidth;
$this->ask = $ask;
}
Expand Down Expand Up @@ -106,10 +103,10 @@ private function table()
$userArray = [];
$this->cursorEnd[0] = count($this->userList);
foreach ($this->userList as $row => $user) {
foreach ($this->getSerializer()->normalize($this->normalizer->normalize($user)) as $col => $item) {
$userArray[$row][array_keys($this->normalizer->normalize($user))[$col]] = (($this->cursor == [$row, $col]) ? "> " : " ") . $item;
foreach (StaticMethods::getSerializer()->normalize(StaticMethods::getSerializer()->normalize($user)) as $col => $item) {
$userArray[$row][array_keys(StaticMethods::getSerializer()->normalize($user))[$col]] = (($this->cursor == [$row, $col]) ? "> " : " ") . $item;
}
$this->cursorEnd[1] = count($this->normalizer->normalize($user));
$this->cursorEnd[1] = count(StaticMethods::getSerializer()->normalize($user));
}
if (!$userArray) {
$this->output->writeln([
Expand Down Expand Up @@ -211,26 +208,17 @@ private function renderFrame(string $frame)
$this->output->write($frame);
}

/**
* @return Serializer
*/
private function getSerializer(): Serializer
{
$normalizers = [new UserEntityNormalizer()];

return new Serializer($normalizers);
}

private function updateValue(): void
{
$io = new SymfonyStyle($this->input, $this->output);
$userReflection = new \ReflectionClass($this->ask->getUserClassName());
$user = $this->userList[$this->cursor[0]];
try {
$property = $userReflection->getProperty(array_keys($this->normalizer->normalize($user))[$this->cursor[1]]);
$annotation = StaticMethods::getRegisterCommand($this->ask->getUserClassName(), array_keys(StaticMethods::getSerializer()->normalize($user))[$this->cursor[1]]);
$userReflection = new \ReflectionClass($this->ask->getUserClassName());
$property = $userReflection->getProperty(array_keys(StaticMethods::getSerializer()->normalize($user))[$this->cursor[1]]);
if (
$this->ask->getReader()->getPropertyAnnotation($property, RegisterCommand::class) &&
$this->ask->getReader()->getPropertyAnnotation($property, RegisterCommand::class)->field
$annotation &&
$annotation->field
) {
$property->setAccessible(true);
$property->setValue($user, $this->ask->ask($property->getName()));
Expand Down Expand Up @@ -265,7 +253,7 @@ private function deleteUser()
}
}
foreach ($userReflection->getProperties() as $property) {
$annotation = $this->ask->getReader()->getPropertyAnnotation($property, RegisterCommand::class);
$annotation = StaticMethods::getRegisterCommand($this->ask->getUserClassName(), $property->getName());
if ($annotation && $annotation->userIdentifier == true) {
$property->setAccessible(true);
$identifier = $property->getValue($user);
Expand Down
1 change: 0 additions & 1 deletion src/Services/UserEditorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function __construct(
OutputInterface $output,
EntityManagerInterface $em,
array $userList,
NormalizerInterface $normalizer,
int $colWidth,
Ask $ask
);
Expand Down

0 comments on commit 67b157a

Please sign in to comment.