foldershare-8.x-1.2/src/Entity/FolderShareTraits/GetSetSizeTrait.php
src/Entity/FolderShareTraits/GetSetSizeTrait.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | <?php namespace Drupal\foldershare\Entity\FolderShareTraits; use Drupal\foldershare\ManageLog; /** * Get/set FolderShare entity size field. * * This trait includes get and set methods for FolderShare entity size field, * along with methods to traverse the folder tree and update the field * the field. * * <B>Internal trait</B> * This trait is internal to the FolderShare module and used to define * features of the FolderShare entity class. It is a mechanism to group * functionality to improve code management. * * @ingroup foldershare */ trait GetSetSizeTrait { /*--------------------------------------------------------------------- * * Size field. * *---------------------------------------------------------------------*/ /** * Clears this item's storage size to empty, if it is a folder. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * For file, image, and media items, the size field mirrors an equivalent * size stored with the underlying File or Media entity. The mirror copy * is made when the FolderShare entity is created and does not change * thereafter. * * For folder items, the size field is the sum of size fields for all * descendants. The field is initialized to zero when the folder is * created. It is cleared to NULL here to flag that the size needs to be * recalculated due to changes in the folder's descendants. Size * recalculation can be done at the end of a series of descendant * operations (such as move, copy, and delete) or by a work queue task. * * If this item is not a folder, this method has no effect. * * The caller must call save() for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @see ::getSize() * @see ::setSize() * @see ::updateSizeAndAncestors() */ private function clearSize() { if ( $this ->isFolder() === TRUE) { $this ->set( 'size' , NULL); } } /** * {@inheritdoc} */ public function getSize() { $value = $this ->get( 'size' )->getValue(); if ( empty ( $value ) === TRUE) { return FALSE; } return $value [0][ 'value' ]; } /** * Sets the item's storage size, in bytes. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The size value is not validated. It is presumed to be a non-negative * value for items with a size, and negative if the size field should be * cleared. Size fields are normally only cleared for folders in order * to indicate that the folder's descendant sizes need to be recalculated. * * The caller must call save() for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should use appropriate * locks before modifying this entity. * * @param int $size * The non-negative size in bytes, or negative to clear the field. * * @see ::clearSize() * @see ::getSize() * @see ::updateSizeAndAncestors() */ private function setSize(int $size ) { if ( $size < 0) { $this ->set( 'size' , NULL); } else { $this ->set( 'size' , $size ); } return $this ; } /*--------------------------------------------------------------------- * * Size updates. * *---------------------------------------------------------------------*/ /** * Updates sizes for this item and its ancestors. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * If this item is a non-hidden folder, its size is set to the sum of * the sizes of its immediate non-hidden children. * * Thereafter, each of this item's ancestors is updated to set its size * to the sum of the sizes of its immediate non-hidden children. * * <B>Process locks</B> * This method does not lock anything. The caller MUST have locked the * root folder tree containing this item. * * @see ::clearSize() * @see ::getSize() * @see ::setSize() */ private function updateSizeAndAncestors() { try { // Get a list of ancestors. $ancestorIds = array_reverse ( $this ->findAncestorFolderIds()); // If this item is a folder, update its size. if ( $this ->isFolder() === TRUE && $this ->isSystemHidden() === FALSE) { $this ->setSize( $this ->findChildrenNumberOfBytes()); $this ->save(); } // Loop over the ancestors from here to the root. Set each one's // size to the sum of their immediate children. foreach ( $ancestorIds as $ancestorId ) { $ancestor = self::load( $ancestorId ); if ( $ancestor === NULL) { // Item does not exist? continue ; } // Set the size to the sum of all children's sizes. $ancestor ->setSize( $ancestor ->findChildrenNumberOfBytes()); $ancestor ->save(); unset( $ancestor ); } } catch (\Exception $e ) { ManageLog::exception( $e ); } } } |