<?php
namespace Claromentis\Pages\UseCase;

$GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"] ?: array()) + array("_e167da360aa15da" . "9eefff030749f85e" . "f" => base64_decode("VGhlIHVwZGF0ZWQgcGFnZSBjb250ZW" . "50IGlzIHB1Ymxpc2hlZA=="), "_f6ab5a050196eebba1afb107ad0143" . "d0" => base64_decode("Y2hlY2tvd" . "XQ="), "_3f05c7e0" . "33d5fa588" . "3b27e9248" . "e3ab53" => base64_decode("cG" . "Fn" . "ZX" . "M="), "_8142def44b242bc756c812f4ab96" . "a3ea" => base64_decode("VGhlIHBhZ2UgaXMgbm90IGNoZWNrZWQgb3V0IC0gbm90aGluZyB0byBwdWJs" . "aXNo"), "_d1de6d14d3d" . "23752c1f5175" . "3f403b72e" => base64_decode("Y2hlY2tpbg=" . "="), "_f7e58afec3ec8739e03ac554a7" . "5d1ffa" => base64_decode("UGFnZSBlZGl0a" . "W5nIGNhbmNlbG" . "xlZA=="), "_017e0023480ae2b406f84af6c66" . "f5110" => base64_decode("UGFnZSBjaGV" . "ja2VkIG91dA" . "=="));
use Claromentis\Core\Audit\Audit;
use Claromentis\Core\Security\SecurityContext;
use Claromentis\Pages\Exception\AccessDeniedException;
use Claromentis\Pages\Exception\InvalidObjectStateException;
use Claromentis\Pages\Exception\WorkingCopyExistsException;
use Claromentis\Pages\Facade\Drafts;
use Claromentis\Pages\Lock\LockService;
use Claromentis\Pages\Model\Page;
use Claromentis\Pages\Model\PageUrl;
use Claromentis\Pages\PagesAcl;
use Claromentis\Pages\Search\Indexing\PageIndexing;
use Claromentis\Search\Indexing\IndexingQueue;




class CheckoutPublish
{
    protected $drafts;
    protected $acl;
    protected $locks;
    protected Audit $audit;
    public function __construct(Drafts $_ca650641449d5d94bb77fc46da77ed8a, PagesAcl $acl, LockService $_9cd4b2152729fd679e63852ebc623594, Audit $audit)
    {
        $this->drafts = $_ca650641449d5d94bb77fc46da77ed8a;
        $this->acl = $acl;
        $this->locks = $_9cd4b2152729fd679e63852ebc623594;
        $this->audit = $audit;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function Publish(SecurityContext $security, PageUrl $page_url)
    {
        $_8c02736ffb2efc1d69d2c9a2a34e7294 = $this->drafts->GetWorkingCopy($page_url, $security->GetUserId());
        if (!$this->acl->CanEditPage($security, $_8c02736ffb2efc1d69d2c9a2a34e7294)) {
            throw new AccessDeniedException();
        }
        
        
        $this->locks->Lock($page_url->id, $security->GetUserId());
        if (!$_8c02736ffb2efc1d69d2c9a2a34e7294) {
            throw new InvalidObjectStateException($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_8142def44b242bc756c812f4" . "ab96a3ea"]);
        }
        $this->drafts->Publish($page_url, $_8c02736ffb2efc1d69d2c9a2a34e7294, $security);
        $this->locks->Release($page_url->id, $security->GetUserId());
        $this->audit->Success($GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_3f05c7e033d5fa5883b27e" . "9248e3ab53"], $GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_d1de6d14d3d23752c" . "1f51753f403b72e"], $page_url->id, $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_e167da360a" . "a15da9eefff" . "030749f85ef"]);
        IndexingQueue::Reindex(PageIndexing::TYPE_NAME, $page_url->id);
    }
    
    
    
    
    
    
    
    
    
    
    public function GetWorkingCopy(SecurityContext $security, PageUrl $page_url)
    {
        if (!$this->acl->CanEditDraft($security, $page_url)) {
            throw new AccessDeniedException();
        }
        return $this->drafts->GetWorkingCopy($page_url, $security->GetUserId());
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function Checkout(SecurityContext $security, PageUrl $page_url)
    {
        if (!$this->acl->CanEditDraft($security, $page_url)) {
            throw new AccessDeniedException();
        }
        $this->locks->Lock($page_url->id, $security->GetUserId());
        $_8c02736ffb2efc1d69d2c9a2a34e7294 = $this->drafts->GetWorkingCopy($page_url, $security->GetUserId());
        if ($_8c02736ffb2efc1d69d2c9a2a34e7294 !== null) {
            throw new WorkingCopyExistsException();
        }
        $_8c02736ffb2efc1d69d2c9a2a34e7294 = $this->drafts->CreateWorkingCopy($page_url, $security->GetUserId());
        $this->drafts->DeleteWorkingCopiesExcept($page_url, $_8c02736ffb2efc1d69d2c9a2a34e7294);
        $this->audit->Success($GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_3f05c7e033d5fa" . "5883b27e9248e3a" . "b53"], $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_f6ab5a050196eebba" . "1afb107ad0143d0"], $page_url->id, $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_017e0023480ae2b40" . "6f84af6c66f5110"]);
        return $_8c02736ffb2efc1d69d2c9a2a34e7294;
    }
    
    
    
    
    
    
    
    
    public function CancelEditing(SecurityContext $security, PageUrl $page_url)
    {
        if (!$this->acl->CanEditDraft($security, $page_url)) {
            throw new AccessDeniedException();
        }
        $_8c02736ffb2efc1d69d2c9a2a34e7294 = $this->drafts->GetWorkingCopy($page_url, $security->GetUserId());
        if ($_8c02736ffb2efc1d69d2c9a2a34e7294 !== null) {
            $this->drafts->DeleteWorkingCopy($page_url, $_8c02736ffb2efc1d69d2c9a2a34e7294);
        }
        $this->audit->Failure($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_3f05c7e033d5fa5883b27e9248e3a" . "b53"], $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_f6ab5a050196eebb" . "a1afb107ad0143d0"], $page_url->id, $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_f7e58afec3ec8739e" . "03ac554a75d1ffa"]);
        $this->locks->Release($page_url->id, $security->GetUserId());
    }
}