ldc-crm-client/src/WordPress/WPCrmQuestion.php
2021-08-12 14:49:27 +02:00

111 lines
3.2 KiB
PHP

<?php
/**
* LDCCRM For Wordpress usage
*
* @category API
* @package None
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
* @license MIT https://opensource.org/licenses/MIT
* @link /
*/
namespace LDC\LDCCRM\WordPress;
use LDC\LDCCRM\Models\Question;
use ReflectionClass;
/**
* Class de gestion de la segmentation question avec Wordpress (ce truc immonde)
*
* @category API
* @package LDCCrmClient
* @subpackage Wordpress
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
* @license MIT https://opensource.org/licenses/MIT
* @link /
*/
abstract class WPCrmQuestion
{
/**
* Get all questions only
*
* @return array
*
* with \LDC\LDCCRM\Models\Question[]
* @throws \LDC\LDCCRM\Exceptions\CrmException
* if an error occurs on server side or on getting response
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException
* If any error or \LDC\LDCCRM\Enums\ApiError occurs
*/
public function getAll(): array
{
return Question::all();
}
/**
* Get all the questions with user answers
*
* @param \LDC\LDCCRM\WordPress\WPCrmUser $user
* @return array
*
* [
* 0 => [
* 'question' => \LDC\LDCCRM\Models\Question,
* 'customerAnswers' => \LDC\LDCCRM\Models\Answer[]
* ]
* ]
*
* with \LDC\LDCCRM\Models\Question[]
* and the customer \LDC\LDCCRM\Models\Answer[]
* @throws \LDC\LDCCRM\Exceptions\CrmException
* if an error occurs on server side or on getting response
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException
* If any error or \LDC\LDCCRM\Enums\ApiError occurs
*/
public function allWithCustomerAnswers(WPCrmUser $user): array
{
if (!self::getRawProperty($user, 'customer')) {
WPCrm::getUserFromApi($user->hashcode);
}
/** @var \LDC\LDCCRM\Models\Customer */
$customer = self::getRawProperty($user, 'customer');
return Question::allWithCustomerAnswers($customer);
}
/**
* Get unanswered question for a specific customer
*
* @param \LDC\LDCCRM\WordPress\WPCrmUser $user
* @return \LDC\LDCCRM\Models\Question[]
* @throws \LDC\LDCCRM\Exceptions\CrmException
* if an error occurs on server side or on getting response
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException
* If any error or \LDC\LDCCRM\Enums\ApiError occurs
*/
public function unansweredBy(WPCrmUser $user): array
{
if (!self::getRawProperty($user, 'customer')) {
WPCrm::getUserFromApi($user->hashcode);
}
/** @var \LDC\LDCCRM\Models\Customer */
$customer = self::getRawProperty($user, 'customer');
return Question::unansweredBy($customer);
}
/**
* Get a property
*
* @param string $field
* @param mixed $value
* @return mixed
*/
protected static function getRawProperty(object $object, string $field)
{
$refClass = new ReflectionClass(\get_class($object));
$refProp = $refClass->getProperty($field);
$refProp->setAccessible(true);
return $refProp->getValue($object);
}
}