<commit_before><?php
namespace MOC\Varnish\Service;

use TYPO3\Flow\Annotations as Flow;
use TYPO3\Neos\Domain\Service\ContentContext;
use TYPO3\TYPO3CR\Domain\Model\NodeData;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
use TYPO3\TYPO3CR\Domain\Model\NodeType;
use TYPO3\TypoScript\Core\Cache\ContentCache;

/**
 * @Flow\Scope("singleton")
 */
class ContentCacheFlusherService {

	/**
	 * @Flow\Inject
	 * @var VarnishBanService
	 */
	protected $varnishBanService;

	/**
	 * @var array
	 */
	protected $tagsToFlush = array();

	/**
	 * @var array
	 */
	protected $domainsToFlush = array();

	/**
	 * @param NodeInterface $node The node which has changed in some way
	 * @return void
	 */
	public function flushForNode(NodeInterface $node) {
		$this->generateCacheTags($node);
	}

	/**
	 * @param NodeData $nodeData The node which has changed in some way
	 * @return void
	 */
	public function flushForNodeData(NodeData $nodeData) {
		$this->generateCacheTags($nodeData);
	}

	/**
	 * Generates cache tags to be flushed for a node which is flushed on shutdown.
	 *
	 * Code duplicated from Neos' ContentCacheFlusher class
	 *
	 * @param NodeInterface|NodeData $node The node which has changed in some way
	 * @return void
	 */
	protected function generateCacheTags($node) {
		$this->tagsToFlush[ContentCache::TAG_EVERYTHING] = 'which were tagged with "Everything".';

		$nodeTypesToFlush = $this->getAllImplementedNodeTypes($node->getNodeType());
		foreach ($nodeTypesToFlush as $nodeType) {
			/** @var NodeType $nodeType */
			$nodeTypeName = $nodeType->getName();
			$this->tagsToFlush['NodeType_' . $nodeTypeName] = sprintf('which were tagged with "NodeType_%s" because node "%s" has changed and was of type "%s".', $nodeTypeName, $node->getPath(), $node->getNodeType()->getName());
		}

		$this->tagsToFlush['Node_' . $node->getIdentifier()] = sprintf('which were tagged with "Node_%s" because node "%s" has changed.', $node->getIdentifier(), $node->getPath());

		while ($node->getDepth() > 1) {
			$node = $node->getParent();
			if ($node === NULL) {
				break;
			}
			$this->tagsToFlush['DescendantOf_' . $node->getIdentifier()] = sprintf('which were tagged with "DescendantOf_%s" because node "%s" has changed.', $node->getIdentifier(), $node->getPath());
		}

		if ($node instanceof NodeInterface && $node->getContext() instanceof ContentContext) {
			$site = $node->getContext()->getCurrentSite();
			if ($site->hasActiveDomains()) {
				foreach ($site->getDomains() as $domain) {
					if ($domain->getActive()) {
						$this->domainsToFlush[] = $domain->getHostPattern();
					}
				}
			}
		}
	}

	/**<commit_msg>TASK: Remove duplicate domains when publishing<commit_after><?php
namespace MOC\Varnish\Service;

use TYPO3\Flow\Annotations as Flow;
use TYPO3\Neos\Domain\Service\ContentContext;
use TYPO3\TYPO3CR\Domain\Model\NodeData;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
use TYPO3\TYPO3CR\Domain\Model\NodeType;
use TYPO3\TypoScript\Core\Cache\ContentCache;

/**
 * @Flow\Scope("singleton")
 */
class ContentCacheFlusherService {

	/**
	 * @Flow\Inject
	 * @var VarnishBanService
	 */
	protected $varnishBanService;

	/**
	 * @var array
	 */
	protected $tagsToFlush = array();

	/**
	 * @var array
	 */
	protected $domainsToFlush = array();

	/**
	 * @param NodeInterface $node The node which has changed in some way
	 * @return void
	 */
	public function flushForNode(NodeInterface $node) {
		$this->generateCacheTags($node);
	}

	/**
	 * @param NodeData $nodeData The node which has changed in some way
	 * @return void
	 */
	public function flushForNodeData(NodeData $nodeData) {
		$this->generateCacheTags($nodeData);
	}

	/**
	 * Generates cache tags to be flushed for a node which is flushed on shutdown.
	 *
	 * Code duplicated from Neos' ContentCacheFlusher class
	 *
	 * @param NodeInterface|NodeData $node The node which has changed in some way
	 * @return void
	 */
	protected function generateCacheTags($node) {
		$this->tagsToFlush[ContentCache::TAG_EVERYTHING] = 'which were tagged with "Everything".';

		$nodeTypesToFlush = $this->getAllImplementedNodeTypes($node->getNodeType());
		foreach ($nodeTypesToFlush as $nodeType) {
			/** @var NodeType $nodeType */
			$nodeTypeName = $nodeType->getName();
			$this->tagsToFlush['NodeType_' . $nodeTypeName] = sprintf('which were tagged with "NodeType_%s" because node "%s" has changed and was of type "%s".', $nodeTypeName, $node->getPath(), $node->getNodeType()->getName());
		}

		$this->tagsToFlush['Node_' . $node->getIdentifier()] = sprintf('which were tagged with "Node_%s" because node "%s" has changed.', $node->getIdentifier(), $node->getPath());

		while ($node->getDepth() > 1) {
			$node = $node->getParent();
			if ($node === NULL) {
				break;
			}
			$this->tagsToFlush['DescendantOf_' . $node->getIdentifier()] = sprintf('which were tagged with "DescendantOf_%s" because node "%s" has changed.', $node->getIdentifier(), $node->getPath());
		}

		if ($node instanceof NodeInterface && $node->getContext() instanceof ContentContext) {
			$site = $node->getContext()->getCurrentSite();
			if ($site->hasActiveDomains()) {
				$domains = $site->getDomains()->filter(function($domain) {
					return $domain->getActive();
				})->map(function($domain) {
					return $domain->getHostpattern();
				})->toArray();
				$this->domainsToFlush = array_unique(array_merge($this->domainsToFlush, $domains));
			}
		}
	}

	/**
{'fraction_non_alphanumeric': 0.15705838876570583, 'fraction_numerical': 0.003695491500369549, 'mean_word_length': 4.348814229249012, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2011586', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1732, 'n_words': 408}