code: protected function applyAccessMode(int $mode): void
      {
          if ($this->mode === self::MODE_CLOSED) {
  
              // set new mode
              $this->mode = $mode;
              return;
          } elseif ($mode === $this->mode) {
  
              // nothing to do
              return;
          }
  
          // mode-switching detected
          throw new AccessDeniedException('unable to switch access-mode for existing binary file handle', 500);
      }
docs: check current access-mode and set new modes
  @param  int $mode
  @return void
  @throws AccessDeniedException