page_tester-1.0.x-dev/src/PageTesterService.php
src/PageTesterService.php
<?php
namespace Drupal\page_tester;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Orgchart service.
*/
class PageTesterService {
/**
* The database.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The time interface.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $timeInterface;
/**
* The logged in user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* The logger channel factory interface.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* The tests plugin manager.
*
* @var \Drupal\page_tester\TestsPluginManager
*/
protected $testsPluginManager;
/**
* PageTester Service constructor.
*
* @param \Drupal\Core\Database\Connection $connection
* The database.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* Load messenger service.
* @param \Drupal\Component\Datetime\TimeInterface $timeInterface
* The time interface.
* @param \Drupal\Core\Session\AccountInterface $account
* The logged in user.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $factory
* The logger channel factory interface.
* @param \Drupal\page_tester\TestsPluginManager $testsPluginManager
* The pageTester service.
*/
public function __construct(
Connection $connection,
MessengerInterface $messenger,
TimeInterface $timeInterface,
AccountInterface $account,
LoggerChannelFactoryInterface $factory,
TestsPluginManager $testsPluginManager
) {
$this->connection = $connection;
$this->messenger = $messenger;
$this->timeInterface = $timeInterface;
$this->account = $account;
$this->loggerFactory = $factory;
$this->testsPluginManager = $testsPluginManager;
}
public function getComment($id) {
$query = $this->connection->select('page_tester', 'pt');
$query->fields('pt', [
'message',
]);
$query->condition('ptid', $id);
return $query->execute();
}
/**
* Gets comment.
*
* @param integer $test_id
* The test id.
*
* @return array
* List of comments.
*/
public function getComments($test_id) {
$query = $this->connection->select('page_tester', 'pt');
$query->fields('pt', [
'ptid',
'test_number',
'message',
]);
$query->condition('test_id', $test_id);
return $query->execute();
}
/**
* Sets comment.
*
* @param integer $test_id
* The test id.
* @param integer $testNumber
* The test number.
* @param string $comment
* The comment.
*/
public function setComment($test_id, $testNumber, $comment) {
$results = $this->connection->insert('page_tester')->fields(
[
'uid' => $this->account->id(),
'test_id' => $test_id,
'test_number' => $testNumber,
'message' => $comment,
'timestamp' => $this->timeInterface->getRequestTime(),
]
)->execute();
$this->loggerFactory->get('page_tester')->notice("Test Number: {$testNumber}, Comment: {$comment}");
if($results) {
$this->messenger->addStatus('Message: ' . $comment . ', Saved');
} else {
$this->messenger->addError('Save failed.');
}
}
/**
* Deletes comment.
*
* @param integer $id
* The id.
*/
public function deleteComment($id) {
$this->connection->delete('page_tester')
->condition('ptid', $id)
->execute();
$this->messenger->addStatus(t('Comment deleted.'));
}
/**
* Gets the test plugin.
*
* @param integer $testId
* The test id.
*
* @return object
* The test instance.
*/
public function getTestPlugin($testId) {
$pageTesterTests = \Drupal::entityTypeManager()->getStorage('page_tester_test')->load($testId);
$plugin_definitions = $this->testsPluginManager->getDefinitions();
foreach ($plugin_definitions as $plugin_id => $definition) {
if ($plugin_id == $pageTesterTests->test->value) {
return $this->testsPluginManager->createInstance($plugin_id);
}
}
}
/**
* Sets a automated test action.
*
* @param integer $testId
* The test id.
* @param string $action
* The test action.
*
*/
public function setAction($test_id, $action) {
$sessionName = 'page_tests_array_'. $test_id;
$page_tests_array = \Drupal::request()->getSession()->get($sessionName, []);
$page_tests_array['action'] = $action;
$request->getSession()->set($sessionName, $page_tests_array);
}
/**
* Gets the first index of an array.
*
* @param array $array
* The array.
*
* @return string
* The first index of the array
*/
public function getFirstIndex($array) {
reset($array);
return key($array);
}
/**
* Gets the last index of an array.
*
* @param array $array
* The array.
*
* @return string
* The last index of the array
*/
public function getLastIndex($array) {
return array_key_last($array);
}
/**
* Returns the number of tests by test id.
* @TODO Refactor to not hard code plugin ids.
*
* @param string $testId
* The vocabulary machine name.
*
* @return array
* The first test number. The last test number. The number of tests.
*/
public function getTestCount($testId) {
$tests = \Drupal::entityTypeManager()->getStorage('page_tester_test')->load($testId);
/** @var Drupal\Core\Entity\EntityTypeManagerInterface $tests */
switch($tests->test->value) {
case 'tests_organigrams_tree':
$plugin = $this->getTestPlugin($testId);
$testsArray = (array)$plugin->getTests($testId);
return [
$this->getFirstIndex($testsArray),
$this->getLastIndex($testsArray),
count($testsArray)
];
break;
case 'tests_menu_links':
$plugin = $this->getTestPlugin($testId);
$testsArray = (array)$plugin->getTests($testId);
return [
$this->getFirstIndex($testsArray),
$this->getLastIndex($testsArray),
count($testsArray)
];
break;
default:
$testArray = (array)$tests->get('content_type_links')->getValue();
return [
$this->getFirstIndex($testsArray),
$this->getLastIndex($testsArray),
count($testsArray)
];
}
}
}
