CmisConnectionApi.php in CMIS API 8
File
src/CmisConnectionApi.php
View source
<?php
namespace Drupal\cmis;
use Dkd\PhpCmis\Enum\BindingType;
use Dkd\PhpCmis\SessionParameter;
class CmisConnectionApi {
private $config;
private $httpInvoker;
private $parameters;
private $sessionFactory;
private $session;
private $rootFolder;
public function __construct($config = '') {
$this
->checkClient();
$this
->setConfig($config);
}
public function checkClient() {
if (!class_exists('BindingType')) {
$path = drupal_get_path('module', 'cmis');
if (file_exists($path . '/vendor/autoload.php')) {
require_once $path . '/vendor/autoload.php';
}
else {
throw new \Exception('Php CMIS Client library is not properly installed.');
}
}
}
private function setConfig($config_id) {
$storage = \Drupal::entityTypeManager()
->getStorage('cmis_connection_entity');
if ($this->config = $storage
->load($config_id)) {
$this
->setHttpInvoker();
}
}
public function getConfig() {
return $this->config;
}
private function setHttpInvoker() {
$guzzle = [
'defaults' => [
'auth' => [
$this->config
->getCmisUser(),
$this->config
->getCmisPassword(),
],
],
];
$this->httpInvoker = new \GuzzleHttp\Client($guzzle);
}
public function getHttpInvoker() {
return $this->httpInvoker;
}
public function setDefaultParameters() {
$parameters = [
SessionParameter::BINDING_TYPE => BindingType::BROWSER,
SessionParameter::BROWSER_URL => $this
->getConfig()
->getCmisUrl(),
SessionParameter::BROWSER_SUCCINCT => FALSE,
SessionParameter::HTTP_INVOKER_OBJECT => $this
->getHttpInvoker(),
];
$this
->setParameters($parameters);
}
public function setParameters($parameters) {
$this->parameters = $parameters;
$this
->setSessionFactory();
}
public function getParameters() {
return $this->parameters;
}
private function setSessionFactory() {
$this->sessionFactory = new \Dkd\PhpCmis\SessionFactory();
$this
->setRepository();
}
public function getSessionFactory() {
return $this->sessionFactory;
}
private function setRepository() {
$repository_id = $this->config
->getCmisRepository();
if ($repository_id === NULL || $repository_id == '') {
$repositories = $this->sessionFactory
->getRepositories($this->parameters);
$this->parameters[\Dkd\PhpCmis\SessionParameter::REPOSITORY_ID] = $repositories[0]
->getId();
}
else {
$this->parameters[\Dkd\PhpCmis\SessionParameter::REPOSITORY_ID] = $repository_id;
}
$this->session = $this->sessionFactory
->createSession($this->parameters);
$this
->setRootFolder();
}
public function getSession() {
return $this->session;
}
private function setRootFolder() {
$this->rootFolder = $this->session
->getRootFolder();
}
public function getRootFolder() {
return $this->rootFolder;
}
public function getObjectById($id = '') {
if (empty($id)) {
return NULL;
}
if (!empty($this
->validObjectId($id) || !empty($this
->validObjectId($id, 'cmis:document')))) {
$cid = $this->session
->createObjectId($id);
$object = $this->session
->getObject($cid);
return $object;
}
return NULL;
}
public function validObjectId($id, $type = 'cmis:folder', $parentId = '') {
$query = "SELECT * FROM {$type} WHERE cmis:objectId='{$id}'";
if (!empty($parentId)) {
$query .= " and IN_FOLDER('{$parentId}')";
}
$result = $this->session
->query($query);
return $result;
}
public function validObjectName($name, $type = 'cmis:folder', $parentId = '') {
$query = "SELECT * FROM {$type} WHERE cmis:name='{$name}'";
if (!empty($parentId)) {
$query .= " and IN_FOLDER('{$parentId}')";
}
$result = $this->session
->query($query);
return $result;
}
}