I know that Magento has CLI for creating new users, but I need to do that via API rest. And extending all CRUD functionalities.
So I tried to create a new module for do that, but it's not working:
These are the 2 main files:
Api/UsersRepositoryInterface.php
namespace Gsp\Users\Api;
interface UserRepositoryInterface
{
/**
* Retrieve User entity by ID
* @param int $id
* @return \Magento\User\Model\User
*/
public function getById($id);
/**
* Save User
* @param \Magento\User\Model\User
* @return \Magento\User\Model\User
*/
public function save(\Magento\User\Model\User $user);
}
Model/UsersRepository.php
namespace Gsp\Users\Model;
class UserRepository implements \Gsp\Users\Api\UserRepositoryInterface
{
/**
* User model factory
*
* @var \Magento\User\Model\UserFactory
*/
protected $userFactory;
public function __construct(
\Magento\User\Model\UserFactory $userFactory
)
{
$this->userFactory = $userFactory;
}
/**
* Retrieve User entity by ID
* @param int $id
* @return \Magento\User\Model\User
*/
public function getById($id)
{
return $this->userFactory->create()->load($id);
}
/**
* Save User
* @param \Magento\User\Model\User
* @return \Magento\User\Model\User
*/
public function save(\Magento\User\Model\User $user)
{
die('ok');
}
}
When I call the getById method it shows that error:
{
"message": "Getter return type must be specified using @return annotation. See Magento\\User\\Model\\User::getFirstName()",
"trace": "#0 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/Reflection/MethodsMap.php(163): Magento\\Framework\\Reflection\\TypeProcessor->getGetterReturnType(Object(Zend\\Code\\Reflection\\MethodReflection))\n#1 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/Reflection/MethodsMap.php(100): Magento\\Framework\\Reflection\\MethodsMap->getMethodMapViaReflection('\\\\Magento\\\\User\\\\M...')\n#2 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/Reflection/DataObjectProcessor.php(77): Magento\\Framework\\Reflection\\MethodsMap->getMethodsMap('\\\\Magento\\\\User\\\\M...')\n#3 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/Webapi/ServiceOutputProcessor.php(107): Magento\\Framework\\Reflection\\DataObjectProcessor->buildOutputDataArray(Object(Magento\\User\\Model\\User\\Interceptor), '\\\\Magento\\\\User\\\\M...')\n#4 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/Webapi/ServiceOutputProcessor.php(59): Magento\\Framework\\Webapi\\ServiceOutputProcessor->convertValue(Object(Magento\\User\\Model\\User\\Interceptor), '\\\\Magento\\\\User\\\\M...')\n#5 /opt/bitnami/apps/magento/htdocs/vendor/magento/module-webapi/Controller/Rest.php(308): Magento\\Framework\\Webapi\\ServiceOutputProcessor->process(Object(Magento\\User\\Model\\User\\Interceptor), 'Gsp\\\\Users\\\\Api\\\\U...', 'getById')\n#6 /opt/bitnami/apps/magento/htdocs/vendor/magento/module-webapi/Controller/Rest.php(216): Magento\\Webapi\\Controller\\Rest->processApiRequest()\n#7 /opt/bitnami/apps/magento/htdocs/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(37): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#8 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/App/Http.php(135): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#9 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/App/Bootstrap.php(258): Magento\\Framework\\App\\Http->launch()\n#10 /opt/bitnami/apps/magento/htdocs/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http))\n#11 {main}"
}
I don't what it means. But using var_dump(get_class($this->userFactory->create()->load($id)));
I figured out it's not the same class?
It showed me that: "Magento\User\Model\User\Interceptor"
So I'm not retrieving the right class? Why Interceptor?
Best Answer
First
Interceptor
class is the class autogenerated in order to make the Magento plugin works. (Documentation : http://devdocs.magento.com/guides/v2.1/extension-dev-guide/plugins.html)Then you need to use interface class in the return type because you do not know the final class implementation due to injection dependency.
So have a look in the native Magento web api interface :
So try to use this return type.
Then check the Magento 2 implementation of this function, it uses repository in order to retrieve the customer :
It should works now.
Keep in mind to use repository and interface.