Skip to content

Commit

Permalink
Merge pull request #1 from ecomteck/dev
Browse files Browse the repository at this point in the history
add files to module
  • Loading branch information
ecomteck authored Aug 28, 2019
2 parents 36ecbf3 + 4caed21 commit 64c29ca
Show file tree
Hide file tree
Showing 17 changed files with 1,338 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Model/Answers/CreateAnswer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Ecomteck
*
* NOTICE OF LICENSE
*
* This source file is subject to the ecomteck.com license that is
* available through the world-wide-web at this URL:
* https://ecomteck.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Ecomteck
* @package Ecomteck_ProductQuestionsGraphQl
* @copyright Copyright (c) 2019 Ecomteck (https://ecomteck.com/)
* @license https://ecomteck.com/LICENSE.txt
*/

namespace Ecomteck\ProductQuestionsGraphQl\Model\Answers;

use Ecomteck\ProductQuestions\Api\Data\AnswerInterface;
use Ecomteck\ProductQuestions\Api\Data\AnswerInterfaceFactory;
use Ecomteck\ProductQuestions\Api\AnswerRepositoryInterface;
use Magento\Framework\Api\DataObjectHelper;

/**
* Class CreateAnswer
* @package Ecomteck\ProductQuestionsGraphQl\Model\Answers
*/
class CreateAnswer
{
/**
* @var DataObjectHelper
*/
private $dataObjectHelper;

/**
* @var AnswerRepositoryInterface
*/
private $answerRepository;

/**
* @var AnswerInterfaceFactory
*/
private $answerFactory;

/**
* CreateAnswer constructor.
* @param DataObjectHelper $dataObjectHelper
* @param AnswerRepositoryInterface $answerRepository
* @param AnswerInterfaceFactory $answerFactory
*/
public function __construct(
DataObjectHelper $dataObjectHelper,
AnswerRepositoryInterface $answerRepository,
AnswerInterfaceFactory $answerFactory
) {
$this->dataObjectHelper = $dataObjectHelper;
$this->answerRepository = $answerRepository;
$this->answerFactory = $answerFactory;
}

/**
* @param array $args
* @return AnswerInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(array $args): AnswerInterface
{
$answerDataObject = $this->answerFactory->create();
$this->dataObjectHelper->populateWithArray(
$answerDataObject,
$args,
AnswerInterface::class
);

return $this->answerRepository->save($answerDataObject);
}
}
59 changes: 59 additions & 0 deletions Model/Answers/FilterArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Ecomteck
*
* NOTICE OF LICENSE
*
* This source file is subject to the ecomteck.com license that is
* available through the world-wide-web at this URL:
* https://ecomteck.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Ecomteck
* @package Ecomteck_ProductQuestionsGraphQl
* @copyright Copyright (c) 2019 Ecomteck (https://ecomteck.com/)
* @license https://ecomteck.com/LICENSE.txt
*/

namespace Ecomteck\ProductQuestionsGraphQl\Model\Answers;

use Magento\Framework\GraphQl\Query\Resolver\Argument\FieldEntityAttributesInterface;
use Magento\Framework\GraphQl\ConfigInterface;

/**
* Class FilterArgument
* @package Ecomteck\ProductQuestionsGraphQl\Model\Answers
*/
class FilterArgument implements FieldEntityAttributesInterface
{
/** @var ConfigInterface */
private $config;

/**
* FilterArgument constructor.
* @param ConfigInterface $config
*/
public function __construct(ConfigInterface $config)
{
$this->config = $config;
}
/**
* Get the attributes for an entity
*
* @return array
*/
public function getEntityAttributes(): array
{
$fields = [];
/** @var \Magento\Framework\GraphQl\Config\Element\Field $field */
$questionFields = $this->config->getConfigElement('AnswerInterFace')->getFields();
foreach ($questionFields as $field) {
$fields[$field->getName()] = 'String';
}
return array_keys($fields);
}
}
82 changes: 82 additions & 0 deletions Model/Questions/CreateQuestion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Ecomteck
*
* NOTICE OF LICENSE
*
* This source file is subject to the ecomteck.com license that is
* available through the world-wide-web at this URL:
* https://ecomteck.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Ecomteck
* @package Ecomteck_ProductQuestionsGraphQl
* @copyright Copyright (c) 2019 Ecomteck (https://ecomteck.com/)
* @license https://ecomteck.com/LICENSE.txt
*/

namespace Ecomteck\ProductQuestionsGraphQl\Model\Questions;

use Ecomteck\ProductQuestions\Api\Data\QuestionInterface;
use Ecomteck\ProductQuestions\Api\Data\QuestionInterfaceFactory;
use Ecomteck\ProductQuestions\Api\QuestionRepositoryInterface;
use Magento\Framework\Api\DataObjectHelper;

/**
* Class CreateQuestion
* @package Ecomteck\ProductQuestionsGraphQl\Model\Questions
*/
class CreateQuestion
{
/**
* @var DataObjectHelper
*/
private $dataObjectHelper;

/**
* @var QuestionRepositoryInterface
*/
private $questionRepository;

/**
* @var QuestionInterfaceFactory
*/
private $questionFactory;

/**
* CreateQuestion constructor.
* @param DataObjectHelper $dataObjectHelper
* @param QuestionRepositoryInterface $questionRepository
* @param QuestionInterfaceFactory $questionFactory
*/
public function __construct(
DataObjectHelper $dataObjectHelper,
QuestionRepositoryInterface $questionRepository,
QuestionInterfaceFactory $questionFactory
) {
$this->dataObjectHelper = $dataObjectHelper;
$this->questionRepository = $questionRepository;
$this->questionFactory = $questionFactory;
}

/**
* @param array $args
* @return QuestionInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(array $args): QuestionInterface
{
$questionDataObject = $this->questionFactory->create();
$this->dataObjectHelper->populateWithArray(
$questionDataObject,
$args,
QuestionInterface::class
);

return $this->questionRepository->save($questionDataObject);
}
}
59 changes: 59 additions & 0 deletions Model/Questions/FilterArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Ecomteck
*
* NOTICE OF LICENSE
*
* This source file is subject to the ecomteck.com license that is
* available through the world-wide-web at this URL:
* https://ecomteck.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Ecomteck
* @package Ecomteck_ProductQuestionsGraphQl
* @copyright Copyright (c) 2019 Ecomteck (https://ecomteck.com/)
* @license https://ecomteck.com/LICENSE.txt
*/

namespace Ecomteck\ProductQuestionsGraphQl\Model\Questions;

use Magento\Framework\GraphQl\Query\Resolver\Argument\FieldEntityAttributesInterface;
use Magento\Framework\GraphQl\ConfigInterface;

/**
* Class FilterArgument
* @package Ecomteck\ProductQuestionsGraphQl\Model\Questions
*/
class FilterArgument implements FieldEntityAttributesInterface
{
/** @var ConfigInterface */
private $config;

/**
* FilterArgument constructor.
* @param ConfigInterface $config
*/
public function __construct(ConfigInterface $config)
{
$this->config = $config;
}
/**
* Get the attributes for an entity
*
* @return array
*/
public function getEntityAttributes(): array
{
$fields = [];
/** @var \Magento\Framework\GraphQl\Config\Element\Field $field */
$questionFields = $this->config->getConfigElement('QuestionInterFace')->getFields();
foreach ($questionFields as $field) {
$fields[$field->getName()] = 'String';
}
return array_keys($fields);
}
}
93 changes: 93 additions & 0 deletions Model/Resolver/Answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Ecomteck
*
* NOTICE OF LICENSE
*
* This source file is subject to the ecomteck.com license that is
* available through the world-wide-web at this URL:
* https://ecomteck.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Ecomteck
* @package Ecomteck_ProductQuestionsGraphQl
* @copyright Copyright (c) 2019 Ecomteck (https://ecomteck.com/)
* @license https://ecomteck.com/LICENSE.txt
*/
declare(strict_types=1);

namespace Ecomteck\ProductQuestionsGraphQl\Model\Resolver;

use Ecomteck\ProductQuestionsGraphQl\Model\Resolver\DataProvider\Answer as AnswerDataProvider;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Class Answer
* @package Ecomteck\ProductQuestionsGraphQl\Model\Resolver
*/
class Answer implements ResolverInterface
{
/**
* @var AnswerDataProvider
*/
private $answerDataProvider;

/**
* Question constructor.
* @param AnswerDataProvider $answerDataProvider
*/
public function __construct(AnswerDataProvider $answerDataProvider)
{
$this->answerDataProvider = $answerDataProvider;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$answerId = $this->getAnswerId($args);
$answerData = $this->getAnswerData($answerId);

return $answerData;
}

/**
* @param array $args
* @return int
* @throws GraphQlInputException
*/
private function getAnswerId(array $args): int
{
if (!isset($args['id'])) {
throw new GraphQlInputException(__('"Answer id should be specified'));
}

return (int)$args['id'];
}

/**
* @param int $answerId
* @return array
* @throws GraphQlNoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getAnswerData(int $answerId): array
{
try {
$answerData = $this->answerDataProvider->getData($answerId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
return $answerData;
}
}
Loading

0 comments on commit 64c29ca

Please sign in to comment.